Inferring the call signature of a lambda or arbitrary callable for “make_function”

前端 未结 3 731
既然无缘
既然无缘 2020-11-28 09:01

In some situations it\'s desirable to be able to type-erase a callable (e.g. function, function pointer, object instance with operator(), lambda, mem_fn

3条回答
  •  北海茫月
    2020-11-28 09:11

    For non-variadic non-generic captureless lambda functions as well as simple free functions one can use following approach:

    #include 
    
    #include 
    
    template< typename L, typename R, typename ...A >
    constexpr
    auto // std::function< R (A...) >
    to_function_pointer(L l, R (L::*)(A...) const)
    {
        return static_cast< R (*)(A...) >(l);
    }
    
    template< typename L, typename R, typename ...A >
    constexpr
    auto // std::function< R (A...) >
    to_function_pointer(L l, R (L::*)(A...)) // for mutable lambda
    {
        return static_cast< R (*)(A...) >(l);
    }
    
    template< typename L >
    constexpr
    auto
    to_function_pointer(L l)
    {
        return to_function_pointer(l, &L::operator ());
    }
    
    template< typename R, typename ...A >
    constexpr
    auto // std::function< R (A...) >
    to_function_pointer(R (* fp)(A...))
    {
        return fp;
    }
    
    namespace
    {
    
    void f() { std::cout << __PRETTY_FUNCTION__ << std::endl; }
    
    }
    
    int
    main()
    {
        to_function_pointer([] () { std::cout << __PRETTY_FUNCTION__ << std::endl; })();
        //to_function_pointer([&] () { std::cout << __PRETTY_FUNCTION__ << std::endl; })(); // can't cast from non-captureless lambda to function pointer
        to_function_pointer([] () mutable { std::cout << __PRETTY_FUNCTION__ << std::endl; })();
        to_function_pointer(f)();
        to_function_pointer(&f)();
        return EXIT_SUCCESS;
    }
    

提交回复
热议问题