c++11: How to write a wrapper function to make `std::function` objects

前端 未结 3 1408
温柔的废话
温柔的废话 2020-12-09 05:50

I am trying to write a wrapper make_function, which like std::make_pair can create a std::function object out of suitable callable obj

3条回答
  •  我在风中等你
    2020-12-09 06:40

    In general you cannot solve it without the severe restriction that whatever you pass to make_function is only callable with exactly one signature.

    What are you going to do with something like:

    struct Generic
    {
        void operator()() { /* ... */ }
        void operator()() const { /* ... */ }
    
        template
        T operator()(T&& t, Ts&&...) { /* ... */ }
    
        template
        T operator()(T&& t, Ts&&...) const { /* ... */ }
    };
    

    C++14 generic lambdas will have the same issue.

    The signature in std::function is based on how you plan to call it and not on how you construct/assign it.

    You cannot solve it for std::bind either, as that has indefinite arity:

    void foo() { std::cout << "foo()" << std::endl; }
    //...
    
    auto f = std::bind(foo);
    f();                 // writes "foo()"
    f(1);                // writes "foo()"
    f(1, 2, 3, 4, 5, 6); // writes "foo()"
    

提交回复
热议问题