Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.