Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
266 views
in Technique[技术] by (71.8m points)

c++ - Why do objects returned from bind ignore extra arguments?

Suppose I have a function that takes two arguments,

void f(int x, int y);

and I want to bind one of them. I can use std::bind as follows:

auto partiallyBoundF = std::bind(f, 10, _1);

partiallyBoundF takes only one argument, but I can call it with more than one. The arguments beyond the first don't even have to be of a type that makes any sense:

partiallyBoundF(20, 0);
partiallyBoundF(0, 44, -99, "Hello", 4.5, true, []{});

What is the purpose of permitting objects returned from bind to be passed extra arguments? It allows calling errors to compile that would be rejected anyplace else.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Ignoring extra arguments is a lot simpler to implement, and can actually be useful.

In a typical implementation e.g. libstdc++ (g++), the approach taken is to collect the operator() arguments into a tuple and then let the std::placeholder bind arguments extract them as required. Enforcing argument count would require counting the number of used placeholders, which would be pretty complicated. Note that the bind callable can be a functor with multiple or templated operator() call patterns, so the bind object operator() can't be generated with a single "correct" signature.

Also note that you can write:

std::bind(&foo, std::placeholders::_1, std::placeholders::_3);

i.e. explicitly ignoring the second argument to the bind object. If bind enforced its argument count you would need an additional way to specify that e.g. a fourth argument was also to be ignored.

As for usefulness, consider binding a member signal handler to a signal:

sig.connect(std::bind(&C::on_sig, this, param, std::placeholders::_1));

If sig has extra unwanted emission parameters, then they are simply ignored by the bind object; otherwise, binding the same handler to multiple signals would require writing multiple forwarding wrappers for no real purpose.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...