需要以下代码的帮助,非常困惑哪里出了问题。
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/