【问题标题】:Matching variables in template metaprogramming c++模板元编程C++中的匹配变量
【发布时间】: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


    【解决方案1】:

    如果字符串是可变参数模板的特化,只需通过std::is_same 比较类型是否相等。结果类型是std::integral_constant,以bool 作为值类型——具体来说,std::true_typestd::false_type

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-07
      • 2020-09-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多