`std::enable_if` is function pointer - how?

前端 未结 2 1486
误落风尘
误落风尘 2020-12-31 11:58

I want to use SFINAE to enable a particular template if the user passes a function pointer as a parameter.

I have googled around but found nothing - I also

2条回答
  •  遥遥无期
    2020-12-31 12:21

    No SFINAE is needed to accept a function pointer or a member function pointer. To distinguish function objects from non-callable stuff SFINAE is needed, there's probably no way around this.

    #include 
    #include 
    
    template 
    void moo (Ret (*fp)(Parm...))
    {
        std::cout << "funptr" << std::endl;
    }
    
    template 
    void moo (Ret (Owner::*fp1)(Parm...))
    {
        std::cout << "memfunptr" << std::endl;
    }
    
    template ())
                                (std::forward(std::declval())...))>
    void moo (Funobj functor)
    {
        std::cout << "funobj" << std::endl;
    }
    
    void x1() {}
    struct X2 { void x2() {} };
    struct X3 { void operator()(){} };
    
    
    int main()
    {
        moo(x1);
        moo(&X2::x2);
        moo(X3());
    }
    

提交回复
热议问题