6

I have the following templated function (C++ latest standard is enabled in the compiler - but maybe 17 would be enough).

#include <functional>

template<typename TReturn, typename ...TArgs>
void MyFunction(const std::function<TReturn(TArgs...)>& callback);

int main()
{
    MyFunction(std::function([](int){}));
    MyFunction([](int){});
}

The first call compiles, when I explicitly convert it to std::function, but the second case does not.

In the first case the template deduction is done automatically, the compiler only knows that it shall convert it to some std::function and able to deduce the parameter and return type.

However in the second case it shall(?) also know that the lambda shall be converted to some std::function, but still unable to do it.

Is there a solution to get the second one running? Or can it be that for templates the automatic conversion does not take place at all?

The error message is:

error C2672: 'MyFunction': no matching overloaded function found

error C2784: 'void MyFunction(const std::function<_Ret(_Types...)> &)': could not deduce template argument for 'const std::function<_Ret(_Types...)>

note: see declaration of 'MyFunction'

What I am aiming for is a "python style decorator". So basically this:

template<typename TReturn, typename ...TArgs>
auto MyFunction(std::function<TReturn(TArgs...)>&& callback) -> std::function<TReturn(TArgs...)>
{
     return [callback = std::move(callback)](TArgs... args)->TReturn
     {
          return callback(std::forward<TArgs>(args)...);
    };
}

If I used a template instead of std::function, the how would I deduce the parameter pack and return value? Is there some way to get it from a callable via some "callable traits"?

Community
  • 1
  • 1
user2281723
  • 519
  • 1
  • 5
  • 16

1 Answers1

5

Or can it be that for templates the automatic conversion does not take place at all?

Yes. Implicit conversions won't be considered in template argument deduction.

Type deduction does not consider implicit conversions (other than type adjustments listed above): that's the job for overload resolution, which happens later.

That means given MyFunction([](int){});, the implicit conversion (from lambda to std::function) won't be considered, then the deduction for TReturn and TArgs fails and the invocation attempt fails too.

As the workarounds, you can

  1. Use explicit conversion as you showed
  2. As the comment suggested, just use a single template parameter for functors. e.g.

    template<typename F>
    auto MyFunction2(F&& callback)
    {
         return [callback = std::move(callback)](auto&&... args)
         {
              return callback(std::forward<decltype(args)>(args)...);
         };
    }
    
songyuanyao
  • 169,198
  • 16
  • 310
  • 405
  • I have edited my question to be a bit more specific, what I am aiming for – user2281723 Feb 13 '19 at 07:37
  • There is no implicit conversion from lambda to `std::function`, is there? However, there is implicit conversion of non-capturing lambda to a function pointer, which in turn can be used to initialize the `std::function`. Am I mistaken? – Krzysiek Karbowiak Feb 13 '19 at 07:55
  • @KrzysiekKarbowiak std::function has a [converting constructor](https://en.cppreference.com/w/cpp/utility/functional/function/function) (the 5th one) for it. – songyuanyao Feb 13 '19 at 08:05
  • 1
    @songyuanyao, I should not post comments before finishing my first coffee. How did I miss constructing `std::function` from a callable object (which lambda is), which I used many times, and instead thought only of construction through function pointer? – Krzysiek Karbowiak Feb 13 '19 at 08:07
  • It works now, thanks. JVAPen suggested to check the type with std::is:invocablem but static_assert(std::is_invocable::value, "Wrapped object must be invocable"); always give false and the assert fails – user2281723 Feb 13 '19 at 11:39