专栏首页C++核心准则原文翻译C++核心准则ES.64:使用T{e}记法构造对象

C++核心准则ES.64:使用T{e}记法构造对象

ES.64: Use the T{e}notation for construction

ES.64:使用T{e}记法构造对象

Reason(原因)

The T{e} construction syntax makes it explicit that construction is desired. The T{e} construction syntax doesn't allow narrowing. T{e} is the only safe and general expression for constructing a value of type T from an expression e. The casts notations T(e) and (T)e are neither safe nor general.

T{e}构造语法明确表达希望的构造方式。T{e}构造语法不允许窄化转换。T{e}是从表达式e构造T值的通用且唯一安全的方式。转换记法T(e)和(T)e既不安全也不通用。

Example(示例)

For built-in types, the construction notation protects against narrowing and reinterpretation

对于内置类型,这种构造方式可以防止窄化和数值的重新解释。

void use(char ch, int i, double d, char* p, long long lng)

{
    int x1 = int{ch};     // OK, but redundant
    int x2 = int{d};      // error: double->int narrowing; use a cast if you need to
    int x3 = int{p};      // error: pointer to->int; use a reinterpret_cast if you really need to
    int x4 = int{lng};    // error: long long->int narrowing; use a cast if you need to

    int y1 = int(ch);     // OK, but redundant
    int y2 = int(d);      // bad: double->int narrowing; use a cast if you need to
    int y3 = int(p);      // bad: pointer to->int; use a reinterpret_cast if you really need to
    int y4 = int(lng);    // bad: long long->int narrowing; use a cast if you need to

    int z1 = (int)ch;     // OK, but redundant
    int z2 = (int)d;      // bad: double->int narrowing; use a cast if you need to
    int z3 = (int)p;      // bad: pointer to->int; use a reinterpret_cast if you really need to
    int z4 = (int)lng;    // bad: long long->int narrowing; use a cast if you need to
}

The integer to/from pointer conversions are implementation defined when using the T(e) or (T)e notations, and non-portable between platforms with different integer and pointer sizes.

当使用T(e)或者(T)e记法进行整数和指针之间的转换时,结果随(编译器的,译者注)实现方式而定,并且在不同的整数和指针长度(64bit?32bit?)之间没有移植性。

Note(注意)

Avoid casts (explicit type conversion) and if you must prefer named casts.

避免类型转换(显式类型转换)。如果必须进行转换,则使用命名转换。

Note(注意)

When unambiguous, the T can be left out of T{e}.

如果目的明确,T可以脱离T{e}。

complex<double> f(complex<double>);

auto z = f({2*pi, 1});Note

The construction notation is the most general initializer notation.

构造记法是最常见的初始化记法。

Exception(例外)

std::vector and other containers were defined before we had {} as a notation for construction. Consider:

std::vector和其他容器在可以使用{}作为构造记法之前就已经存在了。考虑下面的代码:

vector<string> vs {10};                           // ten empty strings
vector<int> vi1 {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};  // ten elements 1..10
vector<int> vi2 {10};                             // one element with the value 10

How do we get a vector of 10 default initialized ints?

如何才能得到包含10个已经默认初始化了的元素的vector?

vector<int> v3(10); // ten elements with value 0

The use of () rather than {} for number of elements is conventional (going back to the early 1980s), hard to change, but still a design error: for a container where the element type can be confused with the number of elements, we have an ambiguity that must be resolved. The conventional resolution is to interpret {10} as a list of one element and use (10) to distinguish a size.

使用()而不是{}初始化元素个数是惯例(可以回溯到1980年代),很难改变,但依然是设计错误:当要素类型可能元素个数相混淆(例如整数,译者注)时,我们有必要消除歧义。习惯的做法是将{10}解释为只包含一个元素的列表,而(10)表示元素个数。

This mistake need not be repeated in new code. We can define a type to represent the number of elements:

这个错误没有必要在新代码中继续重复。我们可以定义一个用于表现元素个数的类型。

struct Count { int n; };

template<typename T>
class Vector {
public:
    Vector(Count n);                     // n default-initialized elements
    Vector(initializer_list<T> init);    // init.size() elements
    // ...
};

Vector<int> v1{10};
Vector<int> v2{Count{10}};
Vector<Count> v3{Count{10}};    // yes, there is still a very minor problem

The main problem left is to find a suitable name for Count.

剩下的主要问题是为Count找到一个合适的名称。

Enforcement(实施建议)

Flag the C-style (T)e and functional-style T(e) casts.

表示所有使用C风格(T)e和函数风格T(e)转换的代码。

原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es64-use-the-tenotation-for-construction


觉得本文有帮助?请分享给更多人。

关注微信公众号【面向对象思考】轻松学习每一天!

面向对象开发,面向对象思考!

本文分享自微信公众号 - 面向对象思考(OOThinkingDalian)

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2020-05-26

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • C++核心准则ES.106:不要试图通过使用无符号类型避免负值

    Choosing unsigned implies many changes to the usual behavior of integers, includ...

    面向对象思考
  • C++核心准则ES.105:避免被0除

    The result is undefined and probably a crash.

    面向对象思考
  • C++核心准则C.1:组织相关数据形成结构体或者类

    Ease of comprehension. If data is related (for fundamental reasons), that fact s...

    面向对象思考
  • C语言函数求参数顺序问题

    对于函数func,先求右边x+=2参数,返回x=8,然后计算结果。也就是传递给形参的两个值都是8,返回值为16。

    用户6755376
  • Android开发笔记(九十七)图片的特效处理

    本文讲述的图片特效处理包括:怀旧、光照、光晕、底片、浮雕、模糊、锐化、黑白、冰冻、素描,所有这些特效都是基于一定的算法,对图像每个点的RGB值进行计算,并汇...

    用户4464237
  • Super Pow:如何高效进行模幂运算

    今天来聊一道与数学运算有关的算法题目,LeetCode 372 题 Super Pow,让你进行巨大的幂运算,然后求余数。

    帅地
  • 剑指OFFER之重建二叉树(九度OJ1385)

    题目描述: 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7...

    用户1154259
  • 更新c++学习笔记 第二章

    **先说说什么是重载:**C++ 允许多个函数拥有相同的名字,只要它们的参数列表不同就可以,这就是函数的重载(Function Overloading)。借助重...

    互联网CEO
  • Python3 基础学习之数值进制转换

        这个函数在上篇里表示强转,并没有输入n这个参数。当n不输入的时候默认是n=10。

    ZY_FlyWay
  • 675. Cut Off Trees for Golf Event

    You are asked to cut off all the trees in this forest in the order of tree’s he...

    用户1147447

扫码关注云+社区

领取腾讯云代金券