C ++中的抛出和三元运算符 [英] Throw and ternary operator in C++
问题描述
以下代码使用G ++ 4.6.1编译,但不能与Visual Studio 2008一起编译
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;
事实是Visual Studio编译器执行内部崩溃。
The fact is the Visual Studio compiler performs an internal crash.
我想知道这是标准C ++,为什么它不能用Visual Studio编译,但是用G ++编译?
I want to know if this is standard C++ and why it doesn't compile with Visual Studio, but does with G++?
推荐答案
它是标准的C ++。条件表达式中的then / else表达式中的一个(或两个)允许为throw-expression(C ++ 98 5.16 / 2)。
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).
Visual Studio崩溃时编译它...这似乎是不幸的!
If Visual Studio crashes when compiling it... that would seem to be unfortunate!
这篇关于C ++中的抛出和三元运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!