std :: make_shared是否执行值初始化(GCC和clang不同意)? [英] Does std::make_shared perform value initialization (GCC and clang disagree)?

查看:409
本文介绍了std :: make_shared是否执行值初始化(GCC和clang不同意)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的意思可以通过以下示例进行解释:

What I take to mean can be explained by the following example:

auto p = std::make_shared<int>();

int变量是默认初始化(因此具有垃圾值)还是值初始化(因此具有零值)?我已经在GCC 5.2和clang 3.6上进行了测试,其中前者进行值初始化,而后者进行默认初始化.我想知道标准对此有何看法?我认为,在这种情况下,现代C ++绝对应该执行值初始化.

Is the int variable default initialized (thus have garbage value) or value initialized (thus have a value of zero)? I've tested on GCC 5.2 and clang 3.6 with the former doing value initialization and the latter doing default initialization. I'm wondering what does the standard say about this? In my opinion, modern C++ should definitely perform value initialization in this case.

推荐答案

是.

N3797 20.8.2.2.6

N3797 20.8.2.2.6

分配适合于T类型对象的内存,并构造一个 通过放置新表达式::new (pv) T(std::forward<Args>(args)...)

Allocates memory suitable for an object of type T and constructs an object in that memory via the placement new expression ::new (pv) T(std::forward<Args>(args)...)

所以,这将是

::new (pv) int();

依此类推,例如N3797 8.5.1

And so on by N3797 8.5.1

以表格形式进行的初始化

The initialization that occurs in the forms

T x(a);
T x{a};

以及新表达式(5.3.4)中的方法都称为直接初始化.

as well as in new expressions (5.3.4) is called direct-initialization.

初始化器的语义如下.目标类型是 初始化的对象或引用的类型以及源 type是初始化表达式的类型.如果初始化器是 不是单个(可能用括号括起来的)表达式,源类型是 未定义.

The semantics of initializers are as follows. The destination type is the type of the object or reference being initialized and the source type is the type of the initializer expression. If the initializer is not a single (possibly parenthesized) expression, the source type is not defined.

-如果初始化器为(),则该对象将被值初始化.

— If the initializer is (), the object is value-initialized.

要对类型为T的对象进行值初始化,则意味着:

To value-initialize an object of type T means:

-否则,该对象将被初始化为零.

— otherwise, the object is zero-initialized.

新的clang和GCC都符合该标准: Live

And both new clang and GCC agree with the standard: Live

这篇关于std :: make_shared是否执行值初始化(GCC和clang不同意)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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