Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 当使用enable_if:”时如何解决此错误;没有名为‘;类型’;在‘;struct std::如果<;虚假、无效>’&引用;_C++_C++11_Templates_Enable If - Fatal编程技术网

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
yield
false

对于
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)