【发布时间】:2021-03-25 16:48:29
【问题描述】:
我想用T的不同类型调用pushArg()方法。
这里是相关代码sn-p:
//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;
}
}
调用pushArg()的函数:
int foo(Struct Demo *) {return 0;}
Ctx ctx;
ctx.pushArg(foo);
以下是错误信息:
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,
^
【问题讨论】:
标签: c++ c++11 templates enable-if