Skip to content

Commit

Permalink
Fix typo in item27
Browse files Browse the repository at this point in the history
  • Loading branch information
ANOLASC authored and y1yang0 committed Feb 24, 2022
1 parent 86e04b0 commit b7bd82f
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion 5.RRefMovSemPerfForw/item27.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ void logAndAddImpl(int idx, std::true_type) //译者注:高亮std::true_type
*tag dispatch*的关键是存在单独一个函数(没有重载)给客户端API。这个单独的函数分发给具体的实现函数。创建一个没有重载的分发函数通常是容易的,但是[Item26](https://github.com/kelthuzadx/EffectiveModernCppChinese/blob/master/5.RRefMovSemPerfForw/item26.md)中所述第二个问题案例是`Person`类的完美转发构造函数,是个例外。编译器可能会自行生成拷贝和移动构造函数,所以即使你只写了一个构造函数并在其中使用*tag dispatch*,有一些对构造函数的调用也被编译器生成的函数处理,绕过了分发机制。
实际上,真正的问题不是编译器生成的函数会绕过*tag diapatch*设计,而是不**总**会绕过去。你希望类的拷贝构造函数总是处理该类型的左值拷贝请求,但是如同[Item26](https://github.com/kelthuzadx/EffectiveModernCppChinese/blob/master/5.RRefMovSemPerfForw/item26.md)中所述,提供具有通用引用的构造函数,会使通用引用构造函数在拷贝non-`const`左值时被调用(而不是拷贝构造函数)。那个条款还说明了当一个基类声明了完美转发构造函数,派生类实现自己的拷贝和移动构造函数时会调用那个完美转发构造函数,尽管正确的行为是调用基类的拷贝或者移动构造。
实际上,真正的问题不是编译器生成的函数会绕过*tag dispatch*设计,而是不**总**会绕过去。你希望类的拷贝构造函数总是处理该类型的左值拷贝请求,但是如同[Item26](https://github.com/kelthuzadx/EffectiveModernCppChinese/blob/master/5.RRefMovSemPerfForw/item26.md)中所述,提供具有通用引用的构造函数,会使通用引用构造函数在拷贝non-`const`左值时被调用(而不是拷贝构造函数)。那个条款还说明了当一个基类声明了完美转发构造函数,派生类实现自己的拷贝和移动构造函数时会调用那个完美转发构造函数,尽管正确的行为是调用基类的拷贝或者移动构造。
这种情况,采用通用引用的重载函数通常比期望的更加贪心,虽然不像单个分派函数一样那么贪心,而又不满足使用*tag dispatch*的条件。你需要另外的技术,可以让你确定允许使用通用引用模板的条件。朋友,你需要的就是`std::enable_if`。
Expand Down

0 comments on commit b7bd82f

Please sign in to comment.