std::make_shared 是否执行值初始化(GCC 和 clang 不同意)?
Does std::make_shared perform value initialization (GCC and clang disagree)?
我的意思可以用下面的例子来解释:
auto p = std::make_shared<int>();
int
变量是默认初始化(因此具有垃圾值)还是值初始化(因此具有零值)?我已经在 GCC 5.2 和 clang 3.6 上进行了测试,前者进行值初始化,后者进行默认初始化。我想知道标准对此有何看法?在我看来,现代C++在这种情况下肯定应该进行值初始化。
是。
N3797 20.8.2.2.6
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 以此类推
The initialization that occurs in the forms
T x(a);
T x{a};
as well as in new expressions (5.3.4) is called direct-initialization.
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.
To value-initialize an object of type T
means:
— otherwise, the object is zero-initialized.
而且 new clang 和 GCC 都符合标准:Live
该标准似乎支持您的意见。
从 20.8.2.2.6 开始:
constructs an object in that memory via the placement new-expression ::new (pv) T(std::forward(args)...)
由于 new int() 是值初始化的,与 new int 相比,我希望为零。
我的意思可以用下面的例子来解释:
auto p = std::make_shared<int>();
int
变量是默认初始化(因此具有垃圾值)还是值初始化(因此具有零值)?我已经在 GCC 5.2 和 clang 3.6 上进行了测试,前者进行值初始化,后者进行默认初始化。我想知道标准对此有何看法?在我看来,现代C++在这种情况下肯定应该进行值初始化。
是。
N3797 20.8.2.2.6
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 以此类推
The initialization that occurs in the forms
T x(a); T x{a};
as well as in new expressions (5.3.4) is called direct-initialization.
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.To value-initialize an object of type
T
means:— otherwise, the object is zero-initialized.
而且 new clang 和 GCC 都符合标准:Live
该标准似乎支持您的意见。
从 20.8.2.2.6 开始:
constructs an object in that memory via the placement new-expression ::new (pv) T(std::forward(args)...)
由于 new int() 是值初始化的,与 new int 相比,我希望为零。