C++怎么使用T{e}记法构造对象

发布时间:2021-11-26 13:44:01 作者:iii
来源:亿速云 阅读:85

这篇文章主要介绍“C++怎么使用T{e}记法构造对象”,在日常操作中,相信很多人在C++怎么使用T{e}记法构造对象问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C++怎么使用T{e}记法构造对象”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

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

Reason(原因)

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

Example(示例)

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

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
}

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

Note(注意)

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

Note(注意)

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

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

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

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

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

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

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

Enforcement(实施建议)

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

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

到此,关于“C++怎么使用T{e}记法构造对象”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. 编程入门学习c++和Java哪个比较好
  2. Linux中安装google的libphonenumber c++库方法是什么

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++

上一篇:raspberry model b如何安装ds323模块

下一篇:C#如何实现基于Socket套接字的网络通信封装

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》