c++ - 无法使用 const char * 数组实例化模板

标签 c++ visual-studio c++11 visual-c++ c++14

需要以下代码的帮助,非常困惑哪里出了问题。

template<const char* (&ArrayC)[]>
        class TypeDescriptionBase
        {
        public:
            static const auto getType(int index)
            {
                return getArrayIndex(ArrayC, index);
            }
            template <typename Func>
            static void forEachType(Func&& f)
            {
                for (const auto& type : ArrayC)
                    f(type);
            }
            static auto findTypeIndex(const CString& strType)
            {
                auto it = std::find(std::begin(ArrayC), std::end(ArrayC), strType);
                return static_cast<int>(it == std::end(ArrayC) ? 0 : std::distance(std::begin(ArrayC), it));
            }
        };

        using advancedTypes = TypeDescriptionBase<{ "TypeA", "TpyeB" , "TpyeC", "TpyeD", "TpyeE", "TpyeF", "TpyeG", "TpyeH", "TpyeI", "TpyeJ"}>;

我收到错误 - “expected an expression”,最后一行围绕 const char* 数组的开头。我正在使用 VS2017 进行开发。

最佳答案

您正在尝试使用字符串数组作为模板参数。这在 C++ 中不受支持。即使是单个字符串也不能作为 C++ 中的模板参数。单个字符可以,因为它们是整数类型。

关于c++ - 无法使用 const char * 数组实例化模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48838187/

相关文章:

c++ - 如何在 C++ 中定义采用两个迭代器的模板函数?

visual-studio - 插入多行 block

c++ - libxml2 xmlTextReaderRead 返回 -1

c++ - 创建 sqlite 表不起作用

c++ - srand(time(NULL)) 产生相似的结果

c - 错误 C2059 : syntax error :"->" and ";"

c++ - Rcpp 需要复制构造函数

c++ - 使用可变参数模板的递归元函数

c++ - 如何在两个不同类之间重载 '==' 运算符?

c++ - C++ 中的 getline() - 不需要 _GNU_SOURCE?