Skip to content

Instantly share code, notes, and snippets.

@tgfrerer
Created August 8, 2019 20:58
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 tgfrerer/0863a6baebd7936bb51c4372d82af256 to your computer and use it in GitHub Desktop.
Save tgfrerer/0863a6baebd7936bb51c4372d82af256 to your computer and use it in GitHub Desktop.
#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;
}
@tgfrerer
Copy link
Author

tgfrerer commented Aug 8, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment