Skip to content

Instantly share code, notes, and snippets.

@kim366
Created April 18, 2019 06:03
Show Gist options
  • Select an option

  • Save kim366/c5ccc5bc380d6fa02d34d09e66d9145d to your computer and use it in GitHub Desktop.

Select an option

Save kim366/c5ccc5bc380d6fa02d34d09e66d9145d to your computer and use it in GitHub Desktop.
#include <cassert>
template<typename T>
struct types_match
{
static bool match() { return false; }
};
#define SYNC(cpp_type, type_name) \
template<> \
struct types_match<cpp_type> \
{ \
static auto type() \
{ \
static auto type = type_name; \
return type; \
} \
\
static bool match(const char* other) { return other == type(); } \
};
SYNC(int, "int");
SYNC(double, "double");
int main()
{
assert(types_match<int>::match("int"));
assert(types_match<double>::match("double"));
assert(!types_match<int>::match("double"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment