Deduce template argument from std::function call signature

前端 未结 3 1139
北荒
北荒 2020-11-27 22:10

Consider this template function:

template
ReturnT foo(const std::function& fun)
{
    return fun();
}
         


        
3条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-27 22:57

    std::function bar;
    
    foo(bar); // works just fine
    

    C++ can't deduce the return type from your function bar because it would have to know the type before it could find all the constructors that take your function pointer.

    For example, who's to say that std::function doesn't have a constructor taking a bool (*)()?

提交回复
热议问题