C ++中的对象初始化语法(T obj = {...} vs T obj {...}) [英] Object Initialization Syntax in C++ ( T obj = {...} vs T obj{...} )

查看:246
本文介绍了C ++中的对象初始化语法(T obj = {...} vs T obj {...})的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

两种初始化形式T obj = {...}T obj{...}有什么区别?
最初,我认为T obj = {...}T obj = T{...}的简写,其中将一个临时对象复制到了我们的新对象中.尽管它不执行复制构造函数(复制省略),但需要它的存在和访问权限做到这一点.但是,当我通过使构造函数私有化来阻止对特定类的复制构造函数访问时,没有错误.
这意味着不涉及复制机制.那么'='符号的作用是什么?
我已经提到了以下问题,但由于缺乏解释而感到不满意:
是C ++ 11统一初始化a替代旧式语法?

What is the difference between the two forms of initialization, T obj = {...} and T obj{...}?
I initially thought T obj = {...} was shorthand for T obj = T{...} where a temporary object is copied into our new object. This, although doesn't execute a copy constructor (copy elision), requires its existence and access to it. But when I blocked copy constructor access in this particular class by making the constructor private, there was no error.
This means that there is no copy mechanism involved. So what's the function of the '=' symbol?
I have referred to the following question but was dissatisfied because of the absence of an explanation:
Is C++11 Uniform Initialization a replacement for the old style syntax?

编辑:类似地,int arr[]{...}int arr[] = {...}有区别吗?我在问这个问题,看看是否可以得出统一初始化和列表初始化之间的对比.

On a similar note, is there a difference between int arr[]{...} and int arr[] = {...}? I am asking this to see if I can bring out the contrast between uniform initialization and list initialization.

推荐答案

这些具有几乎完全相同的效果:

These have almost exactly the same effect:

  • T x = { 1, 2, 3 };
  • T x { 1, 2, 3 };
  • T x = { 1, 2, 3 };
  • T x { 1, 2, 3 };

从技术上讲,带有=的版本称为 copy-list-initialization ,另一个版本称为 direct-list-initialization ,但是这两种形式的行为都是由 list-initialization 行为指定.

Technically the version with = is called copy-list-initialization and the other version is direct-list-initialization but the behaviour of both of those forms is specified by the list-initialization behaviour.

区别是:

  • 如果 copy-list-initialization 选择了explicit构造函数,则代码格式错误.
  • 如果Tauto,则:
    • 复制列表初始化推断std::initializer_list<Type_of_element>
    • 直接列表初始化仅允许列表中的单个元素,并推导Type_of_element.
    • If copy-list-initialization selects an explicit constructor then the code is ill-formed.
    • If T is auto, then:
      • copy-list-initialization deduces std::initializer_list<Type_of_element>
      • direct-list-initialization only allows a single element in the list, and deduces Type_of_element.

      更多信息:为什么要使用标准区分直接列表初始化和复制列表初始化?

      如果T是数组类型,则以上内容仍然适用;由于数组列表初始化始终是聚合初始化,因此永远不会选择构造函数,因此两个版本在所有情况下都是相同的.

      If T is an array type then the above still applies; since array list initialization is always aggregate initialization, there is never a constructor selected and so the two versions are the same in all cases.

      T obj = T{...}(不包括auto)与T obj{...}完全相同,因为C ++ 17,即直接列表初始化 .在C ++ 17之前,存在临时的 direct-list-initialization ,然后从该临时的obj复制初始化.

      T obj = T{...} (excluding auto) is exactly the same as T obj{...}, since C++17, i.e. direct-list-initialization of obj. Prior to C++17 there was direct-list-initialization of a temporary, and then copy-initialization of obj from the temporary.

      这篇关于C ++中的对象初始化语法(T obj = {...} vs T obj {...})的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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