简体   繁体   English

什么是C ++中比较晦涩的部分?

[英]What are some of the more obscure parts of C++?

我已经阅读了很多有关C ++的初学者书籍,而且还读了很多,但是C ++较晦涩的方面是什么?或者在哪里可以找到关于C ++的信息/教程?

ADL (aka Koenig Lookup) is pretty obscure, even though people use it without realizing it in every Hello World program. 即使人们在没有在每个Hello World程序中意识到它的情况下使用它, ADL (也称为Koenig查找)还是相当晦涩的。

The " ScopeGuard trick", where const references as return values from functions are bound to the scope of something they're assigned to, is also fairly obscure. ScopeGuard技巧”也相当晦涩,因为const引用作为函数的返回值绑定到它们所分配的对象的作用域。 That article raised awareness about it quite a bit, though. 不过,那篇文章引起了人们的广泛关注。

There are also a few properties and usages of sizeof() that count as obscure, especially when used in macros or template metaprograms. sizeof()的一些属性和用法也算是晦涩的,尤其是在宏或模板元程序中使用时。

The aphorism for the candidate features is "you don't need it very often, but when you need it, you need it bad." 候选功能的格言是“您并不经常需要它,但是当您需要它时,您就需要它。”

  • Placement new 新刊登位置
  • extern "C++" extern“ C ++”
  • local classes 本地班
  • std::allocator std :: allocator
  • mutable, explicit, volatile 易变,明确,易变
  • pointer-to-any-member-of-any-class 指向任何班级任何成员的指针

So, for the people who have had reason to use these features (library authors), they won't be obscure, and for the majority of C++ programmers, they will be unknown. 因此,对于那些有理由使用这些功能的人(库作者)来说,它们不会模糊不清,对于大多数C ++程序员而言,它们将是未知的。

Actually caring about bad_alloc. 实际上关心的是bad_alloc。

no? 没有?

Edit: what I mean is that in many of the sometimes huge C++ projects in which I have had the pleasure to fix bugs, the concept of catching bad_alloc and acting upon it have been missing. 编辑:我的意思是,在我有时很乐意修复错误的许多有时巨大的C ++项目中,缺少bad_alloc并对其执行操作的概念。 That would put it in the "obscure" part of C++, even though it should not be. 即使不是这样,这也将把它放在C ++的“晦涩”部分。

Some obscure C++ features: 一些晦涩的C ++功能:

Very obscure: 非常晦涩:

  • void operator,(); 无效运算符,();
  • precedence of operator||() and operator&&() when short circuiting 短路时运算符||()和运算符&&()的优先级
    logical expressions 逻辑表达
  • what happens when you overload operator&() 重载operator&()时会发生什么
  • order of initialization of stateful virtual inhertiance. 有状态虚拟继承的初始化顺序。
  • deferencing pointers to template members 引用指向模板成员的指针
  • function try blocks 功能尝试块
  • the allocator::rebind syntax allocator :: rebind语法
  • non default behaviour of std::unexpected std :: unexpected的非默认行为
  • std::cout.imbue() std :: cout.imbue()
  • Anything with locales,especially custom facets 任何具有语言环境的内容,尤其是自定义方面
  • overriding via dominance 通过优势压倒一切
  • trigraphs 三部曲

Just to name a few 仅举几个

赫伯·萨特(Herb Sutter)的书籍是该主题的绝佳来源-从http://www.gotw.ca/publications/xc++.htm开始。

No one mentioned what I think it one of the weirdest parts of C++, obviously an after thought, the syntax for the pre-decrement and pre-increment operators. 没有人提到我认为这是C ++最奇怪的部分之一,显然是经过深思熟虑的,pre-decrement和pre-increment运算符的语法。

class A {
public:
  A &operator++() {...} // Post increment
  A &operator--() {...} // Post decrement
};

vs.

class A {
public:
  A &operator++(int) {...} // Pre increment
  A &operator--(int) {...} // Pre decrement
};

Of course post increment means add one but return the previous value and pre increment means add one and return the new value. 当然,后递增意味着加一但返回前一个值,而预递增意味着加一并返回新值。 Eg: 例如:

A a;
f(a++); // Post increment a
f(++a); // Pre increment a

I'm not sure what happens if you try to use or pass the 'int' argument. 我不确定如果您尝试使用或传递'int'参数会发生什么。 You could try to pass it like this: 您可以尝试通过以下方式传递它:

obj.operator++(0);

I suppose that is how you differentiate between the two operators when calling them explicitly. 我想这就是在显式调用它们时如何区分这两个运算符。

Template metaprogramming (an entire litte turing-complete programming language in C++ executed by the compiler) and hacks with the preprocessor can be very hard! 模板元编程 (整个豆蔻图灵完备的编程语言 C ++编译器执行),并与预处理器的黑客可以很辛苦! (You can even create completely new syntax with this - just take a look at boost::lambda ) (您甚至可以使用它创建全新的语法-看看boost::lambda

But I think the most important thing to learn and understand is the STL (C++ standard library) which is inevitably useful but may look somewhat strange. 但是我认为要学习和理解的最重要的事情是STL(C ++标准库),它不可避免地有用,但看起来有些奇怪。

All books explain what exceptions are. 所有书籍都解释了什么是例外。
But very few talk about exception safety and the exception grantees. 但是很少有人谈论例外安全性和例外授予者。

How to use RAII (easy example smart pointers) to make your code exception safe. 如何使用RAII(简单示例智能指针)使代码异常安全。

What are the exception guarantees that you should provide. 您应该提供哪些例外保证。

  • Destructor should grantee that no exceptions escape. 析构函数应准予所有异常未逃脱。
  • Several of the STL algorithms use things like swap which also should grantee no exceptions escape to work correctly in all situations. 几种STL算法使用诸如swap之类的东西,它们也应该不授予例外转义以在所有情况下都能正常工作。
  • etc... 等等...

Not so obvious is the implementation of certain methods in terms of other methods. 某些方法相对于其他方法的实现不是那么明显。

Assignment operator: 分配运算符:
-> implement as a copy construction and swap. ->实施为副本构建和交换。

operator + 运算符+
-> implemented as copy construction and operator += ->实现为复制构造和运算符+ =

operator != 运算符!=
-> implement in terms of operator == ->根据运算符==实现

etc... 等等...

The concept is that the work should be localized in one method and the other methods use this method to do the real work then add their unique twist to it. 概念是,作品应以一种方法进行局部化,而其他方法则使用此方法进行实际工作,然后在其上添加其独特之处。

If you want to learn something obscure about C++, try templates in depth and read Modern C++ Design by Andrei Alexandrescu . 如果您想了解有关C ++的一些晦涩难懂的知识,请深入尝试模板并阅读Andrei Alexandrescu撰写的 Modern C ++ Design This book is classics about template metaprogramming. 这本书是有关模板元编程的经典著作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM