什么C ++成语在C ++ 11中被弃用 [英] what C++ idioms are deprecated in C++11

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

问题描述

有了新的标准,有新的方式做事情,许多都比旧的方式更好,但旧的方式仍然很好。还清楚的是,出于向后兼容性的原因,新标准没有正式弃用。因此,剩下的问题是:



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

$

解决方案

在回答这个问题时,你可以跳过使用自动变量 ol>
  • 最终类:C ++ 11提供 final 指定符以防止类派生

  • C ++ 11 lambdas大大减少了对指定函数对象b $ b
  • 移动构造函数:std :: auto_ptr工作的神奇方式是由于一流的对右值引用的支持,需要更长时间。

  • 安全bool :这是前面提到的。 C ++ 11的显式运算符避免了这种非常常见的C ++ 03成语。

  • 缩减适配:许多C ++ 11 STL包含提供 shrink_to_fit()成员函数,这将消除需要交换

  • 临时基本类:一些旧的C ++库使用这个相当复杂的习语。不再需要移动语义。

  • 输入安全枚举枚举在C ++ 11中非常安全。

  • 禁止堆分配:=删除语法是一种更直接的方式来说明一个特定的功能被明确拒绝。这适用于防止堆分配(即,为成员运算符new删除),防止复制,分配等。

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

  • 一些数值编译时计算,如fibonacci可以使用常规常数表达式

  • result_of :类模板result_of的使用应该被替换为decltype。我认为result_of在可用时使用decltype。

  • In-

  • 在新的C ++ 11代码中,NULL应重新定义为nullptr,但请参阅 STL的演讲,了解他们为什么决定反对。

  • 表情模板狂热分子很高兴有跟踪返回类型函数语法。不超过30行长的返回类型!

  • 我想我会停在那里!


    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:

    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. 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 contains provide 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 explicitely 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 lines long return types!

    I think I'll stop there!

    这篇关于什么C ++成语在C ++ 11中被弃用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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