lundi 14 décembre 2020

How to slove this error when using enable_if: "no type named ‘type’ in ‘struct std::enable_if

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

Aucun commentaire:

Enregistrer un commentaire