Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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;
}
}
Owner

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