The following code compiles with G++ 4.6.1, but not with Visual Studio 2008
return (m_something == 0) ?
throw std::logic_error("Something wrong happened") : m_something;
The fact is the Visual Studio compiler performs an internal crash.
I want to know if this is standard C++ and why it doesn't compile with Visual Studio, but does with G++?
It is standard C++. Either (or both) of the then/else expressions in a conditional expression is allowed to be a throw-expression instead (C++98 5.16/2).
If Visual Studio crashes when compiling it... that would seem to be unfortunate!
Comeau compiles it without errors (here's my minimal compilable test case):
which is pretty good evidence that it's allowed by the standard. So is the fact that MSVC crashes, rather than failing cleanly with an error.
Also, it appears to be fixed in VC++ 2010
and x64 version:
Upgrade your compiler if possible, this is far from the only bug fixed in 2010.
The internal crash can be considered a bug of Visual Studio. A compiler should never crash because of the code being compiled.
This is a very strange usage of the ternary operator, a simple if before the return would be a much preferable idiom:
From the C++11 February Draft
It appears that
throw
counts as evaluating to avoid
, and that this is allowed.