Created
June 7, 2018 15:37
-
-
Save mirekfranc/e564e2d564f1b3db50a74cf1628a0cc5 to your computer and use it in GitHub Desktop.
fooing around with variadic templates...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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