我知道c++ 11中至少有一个变化会导致一些旧代码停止编译:在标准库中引入显式操作符bool(),取代操作符void*()的旧实例。当然,这将破坏的代码可能是本来就不应该有效的代码,但它仍然是一个破坏性的更改:过去有效的程序不再有效。

还有其他重大变化吗?


当前回答

流提取失败的处理方式不同。

例子

#include <sstream>
#include <cassert>

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
}

更改建议

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

标准参考

[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.

实现

GCC 4.8为c++ 11正确输出: 断言' x == -1'失败 GCC 4.5-4.8所有c++ 03的输出如下,这似乎是一个bug: 断言' x == -1'失败 Visual c++ 2008 Express正确输出c++ 03: 断言失败:x == 0 Visual c++ 2012 Express错误地输出了c++ 11,这似乎是一个实现状态问题: 断言失败:x == 0

其他回答

流提取失败的处理方式不同。

例子

#include <sstream>
#include <cassert>

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
}

更改建议

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

标准参考

[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.

实现

GCC 4.8为c++ 11正确输出: 断言' x == -1'失败 GCC 4.5-4.8所有c++ 03的输出如下,这似乎是一个bug: 断言' x == -1'失败 Visual c++ 2008 Express正确输出c++ 03: 断言失败:x == 0 Visual c++ 2012 Express错误地输出了c++ 11,这似乎是一个实现状态问题: 断言失败:x == 0

显式转换运算符的引入如何成为突破性的变化?旧版本仍然和以前一样“有效”。

是的,从操作符void*() const到显式操作符bool() const的变化将是一个破坏性的变化,但前提是它的使用方式内外都是错误的。符合规范的代码不会被打破。

现在,另一个突破性的变化是在聚合初始化期间禁止收缩转换:

int a[] = { 1.0 }; // error

编辑:请记住,std::identity<T>将在c++ 0x中被删除(参见注释)。它是一种方便的结构体,可以使类型相互依赖。由于结构体实际上并没有做太多的事情,这应该可以修复它:

template<class T>
struct identity{
  typedef T type;
};

对容器库进行了大量更改,可以实现更高效的代码,但在一些极端情况下会默默地破坏向后兼容性。

例如,考虑std::vector、默认结构、c++ 0x和破坏性更改。

断改变?

嗯,首先,如果你使用decltype, constexpr, nullptr等作为标识符,那么你可能会遇到麻烦……

auto关键字的含义改变。