加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 积分或枚举类型的T t {x}和T t = {x}之间的差异?

发布时间:2020-12-16 03:08:04 所属栏目:百科 来源:网络整理
导读:在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
在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 initialization和 copy 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.

我也找不到标准中的{}对应物.我希望这是足够的论据来支持没有区别的答案.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读