Skip to content

Instantly share code, notes, and snippets.

@mirekfranc
Created June 7, 2018 15:37
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 mirekfranc/e564e2d564f1b3db50a74cf1628a0cc5 to your computer and use it in GitHub Desktop.
Save mirekfranc/e564e2d564f1b3db50a74cf1628a0cc5 to your computer and use it in GitHub Desktop.
fooing around with variadic templates...
#include <unordered_map>
#include <string>
#include <iostream>
struct foo;
struct bar;
struct baz;
template<typename T> struct uhm;
template<> struct uhm<foo> {
std::string get_public_request_type() { return "foo";}
std::string get() { return "FOO";}
};
template<> struct uhm<bar> {
std::string get_public_request_type() { return "bar";}
std::string get() { return "BAR";}
};
template<> struct uhm<baz> {
std::string get_public_request_type() { return "baz";}
std::string get() { return "BAZ";}
};
template <typename ...T>
std::unordered_map<std::string, std::string> get_type_to_iface()
{
std::unordered_map<std::string, std::string> type_to_iface =
{
std::make_pair(uhm<T>().get_public_request_type(),
uhm<T>().get())...
};
return type_to_iface;
}
int main()
{
const std::unordered_map<std::string, std::string> type_to_iface =
get_type_to_iface<foo, bar, baz>();
std::cout << type_to_iface.at("foo") << std::endl;
std::cout << type_to_iface.at("bar") << std::endl;
std::cout << type_to_iface.at("baz") << std::endl;
return type_to_iface.size();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment