Different overloads with std::function parameters

2020-03-05 02:17发布

I have two overloads of a function foo which take different std::functions which results in an ambiguity issue for the latter when used with the result of a std::bind. I don't understand why only this is ambiguous.

void foo(std::function<void(int)>) {}
void foo(std::function<int()>) {}

void take_int(int) { }
int ret_int() { return 0; }

When using int() with a bind function I get an ambiguity error

foo(std::bind(ret_int)); // ERROR

With the gcc-5.1 error (and similar with clang)

error: call to 'foo' is ambiguous
  foo(std::bind(ret_int));
  ^~~
note: candidate function
void foo(std::function<void(int)>) {}
     ^
note: candidate function
void foo(std::function<int()>) {}

However all of the following work

foo(std::bind(take_int, _1));
foo(take_int);

foo(ret_int);
foo([](){ return ret_int(); });

struct TakeInt {
  void operator()(int) const { }
};

struct RetInt {
  int operator()() const { return 0; }
};

foo(TakeInt{});
foo(RetInt{});

Looking at the std::function constructor

template< class F > 
function( F f );

it would make sense to me that any function with multiple overloads on different std::function types should have ambiguities, but it's only an issue with the call to bind. I then thought "maybe there's some magic happening to handle function types and lambdas and it doesn't deal with actual classes," but it handles those too.

There's a note on en.cppreference that says [since c++14]

This constructor does not participate in overload resolution unless f is Callable for argument types Args... and return type R

1条回答
女痞
2楼-- · 2020-03-05 03:10

The problem exists in how bind is allowed to be called. As cppreference states

If some of the arguments that are supplied in the call to g() are not matched by any placeholders stored in g, the unused arguments are evaluated and discarded.

In other words, you need to pass at least as many arguments as the underlying callable expects.

This means that the following is valid

int f();
auto b = std::bind(f);
b(1, 2, 3); // arguments aren't used

So saying

auto b = std::bind(ret_int)
b(1);

Works, with the 1 discarded, therefore the following is valid, and overload selection becomes ambiguous

std::function<void(int)> f = std::bind(ret_int);

The inverse is not true, however

std::function<int()> f = std::bind(take_int);

because take_int cannot be called with no arguments.

Takeaway: lambda > bind

查看更多
登录 后发表回答