Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "cout <<(_1*3)" mean?

Tags:

c++

boost

cout

I found a simple code:

using namespace boost::lambda;
typedef std::istream_iterator<int> in;
std::for_each(
    in(std::cin), in(), std::cout << (_1 * 3) << " " );

and I found _1 is used to represent each input integer, but how does this _1 work? Anyone knows?

PS: This code is from the first example of BOOST. When I ran the file, I found the for_each will never terminate and it kept read numbers after each "return" click. Any idea why this happened?

like image 939
mmjuns Avatar asked Dec 07 '25 21:12

mmjuns


1 Answers

This looks like a placeholder (also look at this SO question):

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

When used as an argument in a std::bind expression, the placeholder objects are stored in the generated function object, and when that function object is invoked with unbound arguments, each placeholder _N is replaced by the corresponding Nth unbound argument.

The types of the placeholder objects are DefaultConstructible and CopyConstructible, their default copy/move constructors do not throw exceptions, and for any placeholder _N, the type std::is_placeholder<decltype(_N)> is defined and is derived from std::integral_constant<int, N>.

like image 200
SingerOfTheFall Avatar answered Dec 09 '25 13:12

SingerOfTheFall



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!