Why doesn't C++11 implicitly convert lambdas to std::function objects?

前端 未结 3 2006
难免孤独
难免孤独 2021-01-01 16:00

I implemented a generic event emitter class which allows code to register callbacks, and emit events with arguments. I used Boost.Any type erasure to store the callbacks so

3条回答
  •  礼貌的吻别
    2021-01-01 16:18

    The compiler doesn't infer anything because the compiler implements the C++ language, and the language's template argument deduction rules do not allow the deduction in the way you want.

    Here's a simple example that represents your situation:

    template  struct Foo
    {
        Foo(int) {}
    };
    
    template  void magic(Foo const &);
    
    int main()
    {
        magic(10);   // what is T?
    }
    

提交回复
热议问题