在下面的代码上需要帮助,对于什么是错误非常困惑。
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"}>;
我收到错误 - “期望一个表达式”,其中最后一行围绕const char *数组的开头。我正在使用VS2017进行开发。
答案 0 :(得分:2)
您正在尝试使用字符串数组作为模板参数。 C ++不支持此功能。即使单个字符串也不能成为C ++中的模板参数。个别角色可以,因为它们是整体类型。
答案 1 :(得分:0)
您必须在模板中使用整数类型。其中,您有引用,因此您可以使用引用作为模板类型。
template<std::vector<std::string> & temp>
class TypeDescriptionBase
{
public:
static void ListTypes()
{
for (auto const & it: temp)
{
std::cout<<it<<std::endl;
}
}
};
int main()
{
std::vector<std::string> vect;//must not be local as it must have external linkage!
vect.emplace_back("Type1");
vect.emplace_back("Type2");
vect.emplace_back("Type3");
vect.emplace_back("Type4");
TypeDescriptionBase<vect> types;
types.ListTypes();
return 0;
}
输出:
Type1 Type2 Type3 Type4