Throw and ternary operator in C++

后端 未结 4 1387
盖世英雄少女心
盖世英雄少女心 2020-12-03 22:19

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 happene         


        
4条回答
  •  难免孤独
    2020-12-03 23:09

    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:

    if(m_something == 0)
        throw std::logic_error("Something wrong happened");
    
    return m_something;
    

提交回复
热议问题