C++ 当使用enable_if:”时如何解决此错误;没有名为‘;类型’;在‘;struct std::如果<;虚假、无效>’&引用;
我想用不同类型的C++ 当使用enable_if:”时如何解决此错误;没有名为‘;类型’;在‘;struct std::如果<;虚假、无效>’&引用;,c++,c++11,templates,enable-if,C++,C++11,Templates,Enable If,我想用不同类型的T调用pushArg()方法 以下是相关代码片段: //test.hpp struct Demo {int a}; typedef int (*CALL_CFunction)(struct Demo* ); classs Ctx { /*push bool */ template <typename T, typename std::enable_if<std::is_integral<T>::value&
T
调用pushArg()
方法
以下是相关代码片段:
//test.hpp
struct Demo {int a};
typedef int (*CALL_CFunction)(struct Demo* );
classs Ctx
{
/*push bool */
template <typename T,
typename std::enable_if<std::is_integral<T>::value>::type* = nullptr,
typename std::enable_if<std::is_same<T, bool>::value>::type* = nullptr>
int pushArg(T& val)
{
std::std << "push bool" <<std::endl;
return 0;
}
/*push lua_CFunction*/
template <typename T,
typename std::enable_if<std::is_pointer<T>::value>::type* = nullptr,
typename std::enable_if<std::is_same<CALL_CFunction, T>::value>::type* = nullptr>
int pushArg(T& val)
{
std::cout << "push function" << std::endl;
return 0;
}
}
以下是错误信息:
test.cpp:36:22: error: no matching function for call to ‘ctx::pushArg(int (&)(lua_State*))’
pCtx->pushArg(foo);
^
In file included from test.cpp:1:0:
test.hpp:131:9: note: candidate: template<class T, typename std::enable_if<std::is_integral<_Tp>::value>::type* <anonymous>, typename std::enable_if<std::is_same<T, bool>::value>::type* <anonymous> > int ctx::pushLuaArg(T&)
int pushLuaArg(T& val)
^
test.hpp:131:9: note: template argument deduction/substitution failed:
test.hpp:129:76: error: no type named ‘type’ in ‘struct std::enable_if<false, void>’
typename std::enable_if<std::is_integral<T>::value>::type* = nullptr,
^
test.cpp:36:22:错误:调用“ctx::pushArg(int(&)(lua_State*)”没有匹配的函数
pCtx->pushArg(foo);
^
在test.cpp中包含的文件中:1:0:
test.hpp:131:9:注:候选者:模板int-ctx::pushLuaArg(T&)
内部普什鲁阿格(T&val)
^
test.hpp:131:9:注意:模板参数扣除/替换失败:
test.hpp:129:76:错误:“struct std::enable_if”中没有名为“type”的类型
typename std::enable_如果::type*=nullptr,
^
将pushArg
的参数val
声明为通过引用,然后给出ctx.pushArg(foo)代码>,函数到指针的衰减不会发生,t
被推断为函数类型,即int(Demo*)
。对于第二个重载,std::is_pointer::value
和std::is_same::value
yieldfalse
对于std::is_指针
,您可以使用std::is_函数
,但它似乎是多余的。只要std::is__same
就足够了。(如果std::is_same::value
给出true
,那么std::is_function::value
也将是true
)
模板
// ^
int pushArg(T&val)
为什么std::is_same::value
,而不是std::is_same::value
?你能给我详细解释一下吗?@John函数类型和函数指针类型不是一回事。当通过std::is_same
进行比较时,它们必须匹配。
test.cpp:36:22: error: no matching function for call to ‘ctx::pushArg(int (&)(lua_State*))’
pCtx->pushArg(foo);
^
In file included from test.cpp:1:0:
test.hpp:131:9: note: candidate: template<class T, typename std::enable_if<std::is_integral<_Tp>::value>::type* <anonymous>, typename std::enable_if<std::is_same<T, bool>::value>::type* <anonymous> > int ctx::pushLuaArg(T&)
int pushLuaArg(T& val)
^
test.hpp:131:9: note: template argument deduction/substitution failed:
test.hpp:129:76: error: no type named ‘type’ in ‘struct std::enable_if<false, void>’
typename std::enable_if<std::is_integral<T>::value>::type* = nullptr,
^
template <typename T,
typename std::enable_if<std::is_same<CALL_CFunction, T*>::value>::type* = nullptr>
// ^
int pushArg(T& val)