What breaking changes are introduced in C++11?

后端 未结 9 1837
陌清茗
陌清茗 2020-11-22 14:34

I know that at least one of the changes in C++11 that will cause some old code to stop compiling: the introduction of explicit operator bool() in the standard l

9条回答
  •  醉话见心
    2020-11-22 15:28

    Stream extraction failure is treated differently.

    Example

    #include 
    #include 
    
    int main()
    {
       std::stringstream ss;
       ss << '!';
       
       int x = -1;
       
       assert(!(ss >> x)); // C++03 and C++11
       assert(x == -1);    // C++03
       assert(x == 0);     // C++11
    }
    

    Change proposal

    http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3246.html#23

    Standard reference

    [C++03: 22.2.2.1.2/11]: The result of stage 2 processing can be one of

    • A sequence of chars has been accumulated in stage 2 that is converted (according to the rules of scanf) to a value of the type of val. This value is stored in val and ios_base::goodbit is stored in err.
    • The sequence of chars accumulated in stage 2 would have caused scanf to report an input failure. ios_base::failbit is assigned to err. [ed: Nothing is stored in val.]

    [C++11: 22.4.2.1.2/3]: [..] The numeric value to be stored can be one of:

    • zero, if the conversion function fails to convert the entire field. ios_base::failbit is assigned to err.
    • the most positive representable value, if the field represents a value too large positive to be represented in val. ios_base::failbit is assigned to err.
    • the most negative representable value or zero for an unsigned integer type, if the field represents a value too large negative to be represented in val. ios_base::failbit is assigned to err.
    • the converted value, otherwise.

    The resultant numeric value is stored in val.

    Implementations

    • GCC 4.8 correctly outputs for C++11:

      Assertion `x == -1' failed

    • GCC 4.5-4.8 all output for C++03 the following, which would appear to be a bug:

      Assertion `x == -1' failed

    • Visual C++ 2008 Express correctly outputs for C++03:

      Assertion failed: x == 0

    • Visual C++ 2012 Express incorrectly outputs for C++11, which would appear to be a status-of-implementation issue:

      Assertion failed: x == 0

提交回复
热议问题