Reputation: 283
I am writing a class in TMP to do some basic string processing. A string is represented as a class variadic template of chars. I want to test if two strings are equal and, if they are, have an associated type present, but I don't know how I would test if two chars are equal at compile-time. My current code looks as follows but doesn't compile for obvious reasons.
template <typename T, char firstChar, char... Chrs>
class NamedType
{
public:
typedef T Type;
template <char otherFirst, char... OtherChrs>
class TypeIfMatch
{
};
template <firstChar, char... OtherChrs>
class TypeIfMatch
{
public:
typedef NamedType<T, Chrs>::TypeIfMatch<OtherChars>::type type;
};
template <>
class TypeIfMatch
{
public:
typedef Type type;
};
static const char name[sizeof...(Chrs) + 1];
};
template const char NamedType::name[sizeof...(Chrs)+1] = {Chrs..., '\0'};
Upvotes: 0
Views: 259
Reputation: 60999
If the strings are specializations of a variadic template, just compare the types for equality via std::is_same
. The resulting type is an std::integral_constant
with bool
as the value type -- specifically, either std::true_type
or std::false_type
.
Upvotes: 3