[英]Not a class, namespace or enumeration with templates
我试图运行此代码,它将引发错误。 我不知道为什么会这样。
#include <iostream>
#include <tuple>
#include <typeinfo>
template <typename... Args>
struct type_list
{
template <std::size_t N>
using type = typename std::tuple_element<N, std::tuple<Args...>>::type;
};
template<typename... Ts>
bool foo(unsigned int position)
{
type_list<Ts...> x;
return typeid(x::type<0>) == typeid(true);
}
int main()
{
bool r = foo<int, int>(0);
std::cout << std::boolalpha;
std::cout << r;
}
我得到的错误是:
main.cpp: In function 'bool foo(unsigned int)':
main.cpp:16:19: error: 'x' is not a class, namespace, or enumeration
return typeid(x::type<0>) == typeid(true);
^
main.cpp:16:29: error: expected primary-expression before ')' token
return typeid(x::type<0>) == typeid(true);
如错误消息所述, x
是一个对象,而不是类,名称空间或枚举。
我想你要
return typeid(typename type_list<Ts...>::template type<0>) == typeid(true);
或者您可以使用x
decltype
(C ++ 11起)
return typeid(typename decltype(x)::template type<0>) == typeid(true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.