Created
August 8, 2019 20:58
-
-
Save tgfrerer/0863a6baebd7936bb51c4372d82af256 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <iostream> | |
using namespace std; | |
#define COMPONENT( x ) \ | |
struct x { \ | |
static constexpr auto type_id = #x; \ | |
} | |
COMPONENT( ComponentA ); | |
COMPONENT( ComponentB ); | |
COMPONENT( ComponentC ); | |
COMPONENT( ComponentD ); | |
template <typename A> | |
void print_id() { | |
std::cout << A::type_id << std::endl | |
<< std::flush; | |
} | |
template <typename A, typename B, typename... C> | |
void print_id() { | |
print_id<A>(); | |
print_id<B, C...>(); | |
} | |
int main() { | |
print_id<ComponentB>(); | |
std::cout << std::endl | |
<< std::flush; | |
print_id<ComponentA>(); | |
std::cout << std::endl | |
<< std::flush; | |
print_id<ComponentA, ComponentC>(); | |
std::cout << std::endl | |
<< std::flush; | |
print_id<ComponentA, ComponentB, ComponentC, ComponentD>(); | |
std::cout << std::endl | |
<< std::flush; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note that the recurring case has three template type arguments - because the third, an ellipsis, stands for zero or any number of elements.
If we had only two template type arguments (of which the last an ellipsis), then the recurring case would be ambiguous, since both it and the base case, could be read has having one element - as the ellipsis can also mean zero elements.