A previous C++ question asked why int (x) = 0;
is allowed. However, I noticed that even int(x) = 0;
is allowed, i.e. without a space before the (x)
. I find the latter quite strange, because it causes things like this:
using Oit = std::ostream_iterator<int>;
Oit bar(std::cout);
*bar = 6; // * is optional
*Oit(bar) = 7; // * is NOT optional!
where the final line is because omitting the *
makes the compiler think we are declaring bar
again and initializing to 7.
Am I interpreting this correctly, that int(x) = 0;
is indeed equivalent to int x = 0
, and Oit(bar) = 7;
is indeed equivalent to Oit bar = 7;
? If yes, why specifically does C++ allow omitting the space before the parentheses in such a declaration + initialization?
(my guess is because the C++ compiler does not care about any space before a left paren, since it treats that parenthesized expression as it's own "token" [excuse me if I'm butchering the terminology], i.e. in all cases, qux(baz)
is equivalent to qux (baz)
)