|> While porting some C++ code from Digital C++ to Microsoft Visual
|> C++ I game across code that looks like this:
|> int x;
|> bool y = x != 0 ? x : throw std::range_error("aaargh...");
|> VC++ gives the following error:
|> error C2440: 'initializing' : cannot convert from 'void' to 'bool'
|> Expressions of type void cannot be converted to other types
|> Digital C++ compiles it without complaints.
|> Is this valid C++ or a compiler bug in VC++?
It is valid C++, and there is a bug in VC++ for not accepting it.
|> Is throw an expression that returns a void?
Yes. But there is no place where you attempt to convert this void to
bool. What you are attempting to convert to bool is the results of
the ?: operator. And according to the standard (5.16/3):
If either the second or the third operand has type void, [...],
and one of the following shall hold:
-- The second or the third operand (but not both) is a
throw-expression; the result is of the type of the other, and
is an rvalue.
In this case, the result of ?: is of type int, and you can convert an
int to a bool.
Conseils en informatique oriente objet/
Beratung in objektorientierter Datenverarbeitung
Ziegelhttenweg 17a, 60598 Frankfurt, Germany Tel. +49(0)179 2607481
[ about comp.lang.c++.moderated. First time posters: do this! ]