1

So has std::function Deduction Guides so given:

int foo();

I can do:

std::function bar(foo);

But I'm stuck on a compiler. There I have to do something more like: function<int()> bar(foo). I was wondering if there was a way to create a std::function without passing the function pointer and explicitly providing the function signature? So for example make_pair will deduce the type of it's return from it's arguments. I was wondering if I could write something similar for functions even using , like:

auto bar = make_function(foo);

Is this doable?

Note: My real case is that foo is a template function with a lot of arguments I don't want to deduce. So my motivation here is to generate a function without needing to provide the parameter types.

Live Example

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • In your real case, `std::function bar(foo)` doesn't work in [tag:c++17]. Please provide an actual [MCVE] of what you want to do in legal [tag:c++17], and ask for something similar to be implemented in [tag:c++14]. – Yakk - Adam Nevraumont Mar 05 '19 at 02:09
  • @Yakk-AdamNevraumont I'm not certain what you mean about it not working... I've added an example though :shrug: – Jonathan Mee Mar 05 '19 at 13:20
  • **"My real case is that foo is a template function with a lot of arguments"** -- your "[MCVE]" is fundamentally different than your real use case, [as your real use case doesn't work here](https://coliru.stacked-crooked.com/a/7f7be1b22acff917). When writing a [MCVE], start with a *real use case*, then minimize. Don't minimize *first*. – Yakk - Adam Nevraumont Mar 05 '19 at 13:28
  • @Yakk-AdamNevraumont So [this is my original question](https://stackoverflow.com/q/54984669/2642059). You can see my answer at the bottom. My point in the comment was to ask for a way to generate a `function` which could be fully defined by my passing just a function pointer. I thought that was clear? Is my edit working for you? – Jonathan Mee Mar 05 '19 at 13:40
  • @Yakk-AdamNevraumont I'm going to take it from your edit that you are affirming where we're at with this now? – Jonathan Mee Mar 05 '19 at 15:02

1 Answers1

3

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<class R, class... ARGS>
 auto make_func(R (*ptr)(ARGS...)) {
      return std::function<R (*)(ARGS...)>(ptr);
 }

And than you use it:

auto bar = make_func(&foo<Z, Y, Z>);
SergeyA
  • 61,605
  • 5
  • 78
  • 137
  • I'm accepting because this is a genius solution on C++14. But I'm annoyed to say that this still doesn't work on Visual Studio 2017 version 15.6.7: ideone.com/T52Gxn I get this error: "error C2039: `result_type`: is not a member of global namespace" when I try to use `make_func` to obtain the return type in an `enable_if_t`. I'm going to open a new question to see if there is a workaround... – Jonathan Mee Mar 07 '19 at 14:56
  • I've asked my follow up here if you have any suggestions: https://stackoverflow.com/q/55047417/2642059 – Jonathan Mee Mar 07 '19 at 15:30
  • Just a note to any future readers, I needed a function to return a `std::funciton` so I could extract the return type. This is a fantastic answer, and should be accepted for the given question. However if your interests align more closely with mine I've based a C++14 answer off this one: https://stackoverflow.com/a/55050902/2642059 – Jonathan Mee Mar 10 '19 at 23:40