Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why start with std::placeholders::_1 instead of _0?

Most everything in c++ is 0, not 1 based. Just out of curiosity, why are placeholders 1 based? Meaning _1 is the first parameter, not _0.

like image 471
walrii Avatar asked Mar 10 '13 21:03

walrii


People also ask

What is std:: placeholders::_ 1?

The std::placeholders namespace contains the placeholder objects [_1, ..., _N] where N is an implementation defined maximum number.

What is place holder in c++?

Placeholders are namespaces which detect the position of a value in a function. Placeholders are represented by _1, _2, _3 etc.

What are Boost placeholders?

The placeholders provide std::bind compatible placeholders that additionally provide basic C++ operators that creates bind expressions. Each bind expression supports constexpr function evaluation.

How does c++ bind work?

How does bind() work? Bind function with the help of placeholders helps to manipulate the position and number of values to be used by the function and modifies the function according to the desired output.


2 Answers

Because that's how boost::bind does it, and the Boost.Bind author wrote the proposal to add it to TR1 and that got copied into the standard.

As for why Boost.Bind does it that way, I don't know, but I would hazard a guess it might be to match std::bind1st and std::bind2nd from the 1998 standard, which came from the STL. In that context "1st" i.e. "first" is correct (even in a zero-based indexing system the item at index zero is the first, not the zeroth, item.)

So maybe the placeholders should be _1st, _2nd, _3rd, _4th etc. but for non-English speakers who don't know the inconsistent suffixes on ordinal numbers it's probably easier to remember _1, _2 etc.

Just a wild guess though. The question had never occurred to me so now I'm curious too :-)

like image 116
Jonathan Wakely Avatar answered Oct 23 '22 03:10

Jonathan Wakely


An advantage to this is workings of std::is_placeholder. The result isn't just true or false, it's the value of the placeholder itself.

std::is_placeholder<_1>::value == 1
std::is_placeholder<_2>::value == 2
std::is_placeholder<_7>::value == 7

but anything not a placeholder will evaluate to 0 (which is of course, false). If placeholders started at _0 this wouldn't work.

like image 28
Ryan Haining Avatar answered Oct 23 '22 04:10

Ryan Haining