gpt4 book ai didi

c++ - T t {x} 和 T t = { x } 对于整数或枚举类型的区别?

转载 作者:可可西里 更新时间:2023-11-01 16:11:14 26 4
gpt4 key购买 nike

在 C++14 中:

对于任何整数或枚举类型 T 以及对于任何表达式 expr:

有没有区别:

struct S { T t { expr }; };

struct S { T t = { expr }; };

更新:

我得到了 [dcl.init.list]p3b5 上面写着:

If the initializer list has a single element of type E and either T is not a reference type or its referenced type is reference-related to E, the object or reference is initialized from that element.

我相信这句话适用于direct-list-initializationcopy-list-initialization

所以我认为答案是否定的,没有区别。

最佳答案

如果你看一下direct initializationcopy initialization引用文献,你会发现相同的词:

if T is a non-class type, standard conversions are used, if necessary, to convert the value of other to the cv-unqualified version of T

所以应该没有区别。这些初始化的区别仅适用于类类型:复制初始化考虑显式构造函数和显式用户-定义的转换运算符,直接初始化。整型和枚举类型都没有。

编辑:
@ᐅ Johannes Schaub - litb ᐊ answered a relative question to this one (仅关于括号,而不是大括号)并且他引用了 8.5/14 类似的措辞(强调我的):

The form of initialization (using parentheses or =) is generally insignificant, but does matter when the initializer or the entity being initialized has a class type; see below. If the entity being initialized does not have class type, the expression-list in a parenthesized initializer shall be a single expression.

我也无法在标准中找到对应的 {}。我希望这些论据足以支持没有区别的答案。

关于c++ - T t {x} 和 T t = { x } 对于整数或枚举类型的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33832423/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com