Can I generate a function without providing arguments?

前端 未结 1 1871
不知归路
不知归路 2020-12-21 22:09

So c++17 has std::function Deduction Guides so given:

int foo();

I can do:

std::function bar(foo);

But I\

1条回答
  •  情书的邮戳
    2020-12-21 22:24

    Your question has some most important part in the end in the fine print. If your foo is a template, C++17 deduction guides won't help you with a simple syntax like

    std::function f(foo);
    

    You'd still need to provide template arguments for foo. Assuming you are OK with specifying foo's argument types (as you have to be) writing make_func is a trivial exercise:

     template
     auto make_func(R (*ptr)(ARGS...)) {
          return std::function(ptr);
     }
    

    And than you use it:

    auto bar = make_func(&foo);
    

    0 讨论(0)
提交回复
热议问题