Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Stroustrup's example, what does the colon mean in "return 1 : 2"?

Tags:

c++

syntax

It's a typo in the book. Look at Errata for 2nd and 3rd printings of The C++ Programming Language. The example must be like below:

auto z3 =[y]() { return (y) ? 1 : 2; }

Looks to me like a simple typo. Should probably be:

auto z3 =[y]() { return y ? 1 : 2; }

Note that since the lambda doesn't take any parameters, the parens are optional. You could use this instead, if you preferred:

auto z3 =[y] { return y ? 1 : 2; }

return 1 : 2; is a syntax error, it is not valid code.

A correct statement would be more like return (y) ? 1 : 2; instead.