Object obj(args ...)和Object obj {args ...}`有什么区别? [英] What's the difference between `Object obj(args...)` and `Object obj{args...}`?

查看:116
本文介绍了Object obj(args ...)和Object obj {args ...}`有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

草稿书有效的C ++ 11 Scott Meyers指出:

The draft book Effective C++11 by Scott Meyers states:

在创建对象时区分()和{}

Distinguish () and {} when creating objects

Object obj(args...)Object obj{args...}有什么区别?以及斯科特为什么这么说.

What's the difference between Object obj(args...) and Object obj{args...}? and why Scott says so.

更新:

问题如何使用C ++ 11统一初始化语法?询问操作方法,而这个问题询问原因.

The question How to use C++11 uniform initialization syntax? asks for HOW, and this question asks for WHY.

Update2:

我发现以下链接是有帮助的,并完全回答了这个问题:

I find the following link is helpful and completely answers this question:

https://softwareengineering .stackexchange.com/questions/133688/is-c11-uniform-initialization-a-placement for the old-style syntax

推荐答案

Object obj(args...)Object obj{args...}有什么区别?

第一个是 direct-initialization ,第二个是 direct-list-initialization .在两个不同的部分中提到了这一点:

The first is direct-initialization while the second is direct-list-initialization. This is mentioned in two different sections:

§8.5/16 [dcl.init]

以表格形式进行的初始化

The initialization that occurs in the forms

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

以及new表达式(5.3.4),static_cast表达式(5.2.9),功能符号类型转换(5.2.3)以及基和成员初始值设定项(12.6.2)被称为 直接初始化 .

as well as in new expressions (5.3.4), static_cast expressions (5.2.9), functional notation type conversions (5.2.3), and base and member initializers (12.6.2) is called direct-initialization.

§8.5.4/1 [dcl.init.list]

列表初始化是对来自 braised-init-list 的对象或引用的初始化.这样的初始化程序称为初始化程序列表,列表中逗号分隔的 initializer-clauses 称为初始化程序列表的 elements .初始化程序列表可能为空. 列表初始化可以在直接初始化或复制初始化上下文中进行;在直接初始化上下文中的列表初始化称为 direct-list-initialization ,在复制初始化上下文中的列表初始化称为 copy-list-initialization .

List-initialization is initialization of an object or reference from a braced-init-list. Such an initializer is called an initializer list, and the comma-separated initializer-clauses of the list are called the elements of the initializer list. An initializer list may be empty. List-initialization can occur in direct-initialization or copy-initialization contexts; list-initialization in a direct-initialization context is called direct-list-initialization and list-initialization in a copy-initialization context is called copy-list-initialization.


两者之间有一些区别:


There are a few differences between the two:

  • 如果所构造的类型的构造函数带有initializer_list参数,则 direct-list-initialization 将始终偏向于该构造函数.仅当initializer_list构造函数不可行时,才会考虑其他构造函数. §13.3.1.7/1 [over.match.list]

  • If the type being constructed has a constructor that takes an initializer_list argument, direct-list-initialization will always favor that constructor. Other constructors will only be considered if the initializer_list constructor is not viable. §13.3.1.7/1 [over.match.list]

直接列表初始化不允许在参数列表内缩小转换范围. §8.5.4/3 [dcl.init.list]

direct-list-initialization does not allow narrowing conversions within the argument list. §8.5.4/3 [dcl.init.list]

如果要初始化的类型是聚合类型,则 direct-list-initialization 将执行聚合初始化. §8.5.4/3 [dcl.init.list]

If the type being initialized is an aggregate, direct-list-initialization will perform aggregate initialization. §8.5.4/3 [dcl.init.list]

braced-init-list 的元素的评估顺序是从左到右. §8.5.4/4 [dcl.init.list]

The order of evaluation of the elements of a braced-init-list is from left to right. §8.5.4/4 [dcl.init.list]

通过使用直接列表初始化

 

  struct foo{};
  struct bar 
  {    
    bar(foo const&) {}
  };

  bar b1(foo()); // most vexing parse
  bar b2(foo{}); // all 3 of the following construct objects of type bar
  bar b3{foo()};
  bar b4{foo{}};

这篇关于Object obj(args ...)和Object obj {args ...}`有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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