简体   繁体   中英

How to slove this error when using enable_if: “no type named ‘type’ in ‘struct std::enable_if<false, void>’”

I want to invoke pushArg() method with different kinds of the type of T .

Here is the relate code snippet:

//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;
    }
}

The function to call pushArg() :

int foo(Struct Demo *) {return 0;}
Ctx ctx;
ctx.pushArg(foo);

Here are the error meessages:

  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,
                                                                            ^

The parameter val of pushArg is declared as pass-by-reference, then given ctx.pushArg(foo); , function-to-pointer decay won't happen, and T is deduced as function type, ie int (Demo*) . For the 2nd overload, both std::is_pointer<T>::value and std::is_same<CALL_CFunction, T>::value yield false .

For std::is_pointer , you can use std::is_function instead but it seems redundent. Just std::is_same is enough. (If std::is_same<CALL_CFunction, T*>::value gives true then std::is_function<T>::value will be true too.) Eg

template <typename T, 
          typename std::enable_if<std::is_same<CALL_CFunction, T*>::value>::type* = nullptr>
//                                                              ^
int pushArg(T& val)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM