【发布时间】:2014-12-06 20:21:00
【问题描述】:
我正在 TMP 中编写一个类来进行一些基本的字符串处理。字符串表示为字符的类可变参数模板。我想测试两个字符串是否相等,如果相等,是否存在关联类型,但我不知道如何在编译时测试两个字符是否相等。我当前的代码如下所示,但由于明显的原因无法编译。
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'};
【问题讨论】:
标签: c++ variadic-templates template-meta-programming