Skip to content

Instantly share code, notes, and snippets.

@dazfuller
Created November 6, 2011 21:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dazfuller/1343524 to your computer and use it in GitHub Desktop.
Save dazfuller/1343524 to your computer and use it in GitHub Desktop.
Ignoring the Voices: C++11 Example - decltype
#include <iostream>
#include <map>
#include <string>
#include <vector>
std::map<int, std::vector<std::string>> MyFunction()
{
std::vector<std::string> v1;
v1.push_back("Hello");
v1.push_back("World");
std::vector<std::string> v2;
v2.push_back("Goodbye");
v2.push_back("World");
std::map<int, decltype(v2)> m1;
m1.insert(std::pair<int, decltype(v2)>(1, v1));
m1.insert(std::pair<int, decltype(v2)>(2, v2));
return m1;
}
auto MakeCollection() -> decltype( MyFunction() )
{
auto val = MyFunction();
return val;
}
int main()
{
auto collection = MakeCollection();
for (auto m = collection.begin(); m != collection.end(); ++m)
{
std::cout << m->first;
for (auto s = m->second.begin(); s != m->second.end(); ++s)
{
std::cout << " " << *s;
}
std::cout << std::endl;
}
}
@dazfuller
Copy link
Author

Compiled as follows:

g++ -std=c++0x -Wall -Werror -o decltype_useful decltype_useful.cpp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment