Deduce template argument from std::function call signature

A function pointer of type bool (*)() can be converted to std::function<bool()> but is not the same type, so a conversion is needed. Before the compiler can check whether that conversion is possible it needs to deduce ReturnT as bool, but to do that it needs to already know that std::function<bool()> is a possible conversion, which isn’t possible until it deduces ReturnT … see the problem?

Also, consider that bool(*)() could also be converted to std::function<void()> or std::function<int()> … which should be deduced?

Consider this simplification:

template<typename T>
  struct function
  {
    template<typename U>
      function(U) { }
  };

template<typename T>
  void foo(function<T>)
  { }

int main()
{
    foo(1);
}

How can the compiler know whether you wanted to create function<int> or function<char> or function<void> when they can all be constructed from an int?

Leave a Comment