C ++ 11中弃用了哪些C ++习惯用法? [英] Which C++ idioms are deprecated in C++11?

查看:111
本文介绍了C ++ 11中弃用了哪些C ++习惯用法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有了新标准,就有了新的做事方法,许多方法都比旧方法好,但旧方法仍然可以。同样很明显,出于向后兼容的原因,新标准并未正式弃用。因此仍然存在的问题是:

With the new standard, there are new ways of doing things, and many are nicer than the old ways, but the old way is still fine. It's also clear that the new standard doesn't officially deprecate very much, for backward compatibility reasons. So the question that remains is:

哪些旧的编码方式绝对不如C ++ 11样式,我们现在可以做什么?

What old ways of coding are definitely inferior to C++11 styles, and what can we now do instead?

在回答此问题时,您可能会跳过诸如使用自动变量之类的明显内容。

In answering this, you may skip the obvious things like "use auto variables".

推荐答案


  1. 最终类:C ++ 11提供了 final 说明符,以防止类派生

  2. C ++ 11 lambda大大减少了对命名函数对象(functor)类的需求。

  3. 移动构造器:神奇由于对右值引用的一流支持,因此不再需要 std :: auto_ptr 的工作方式。

  4. 安全布尔:前面已经提到过。 C ++ 11的显式运算符消除了这个非常常见的C ++ 03习惯用法。

  5. 缩小至适合:许多C ++ 11 STL容器都提供了 shrink_to_fit()成员函数,

  6. 临时基类:一些旧的C ++库使用了这个相当复杂的习惯用法。有了移动语义,就不再需要它。

  7. 类型安全枚举枚举在C ++ 11中非常安全。

  8. 禁止堆分配 = delete 语法是一种更直接的方式,用于明确拒绝特定功能。这适用于防止堆分配(即,对于成员 operator new 的成员 = delete ),防止复制,分配等

  9. 模板化typedef : C ++ 11中的别名模板减少了对简单模板化typedef的需求。但是,复杂类型生成器仍然需要元函数。

  10. 某些数字编译时计算(例如Fibonacci)可以使用通用常量表达式

  11. result_of :类模板 result_of 应替换为 decltype 。我认为 result_of 在可用时使用 decltype

  12. 类内成员初始化程序保存键入以默认方式初始化非静态成员值。

  13. 在新的C ++ 11代码中, NULL 应该重新定义为 nullptr ,但请参见 STL的谈话,以了解它们为何如此

  14. 表达式模板狂热者很高兴拥有跟踪返回类型函数C ++ 11中的语法。

  1. Final Class: C++11 provides the final specifier to prevent class derivation
  2. C++11 lambdas substantially reduce the need for named function object (functor) classes.
  3. Move Constructor: The magical ways in which std::auto_ptr works are no longer needed due to first-class support for rvalue references.
  4. Safe bool: This was mentioned earlier. Explicit operators of C++11 obviate this very common C++03 idiom.
  5. Shrink-to-fit: Many C++11 STL containers provide a shrink_to_fit() member function, which should eliminate the need swapping with a temporary.
  6. Temporary Base Class: Some old C++ libraries use this rather complex idiom. With move semantics it's no longer needed.
  7. Type Safe Enum Enumerations are very safe in C++11.
  8. Prohibiting heap allocation: The = delete syntax is a much more direct way of saying that a particular functionality is explicitly denied. This is applicable to preventing heap allocation (i.e., =delete for member operator new), preventing copies, assignment, etc.
  9. Templated typedef: Alias templates in C++11 reduce the need for simple templated typedefs. However, complex type generators still need meta functions.
  10. Some numerical compile-time computations, such as Fibonacci can be easily replaced using generalized constant expressions
  11. result_of: Uses of class template result_of should be replaced with decltype. I think result_of uses decltype when it is available.
  12. In-class member initializers save typing for default initialization of non-static members with default values.
  13. In new C++11 code NULL should be redefined as nullptr, but see STL's talk to learn why they decided against it.
  14. Expression template fanatics are delighted to have the trailing return type function syntax in C++11. No more 30-line long return types!

我想我会停在那里!

这篇关于C ++ 11中弃用了哪些C ++习惯用法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆