转:copy initialization

时间:2021-09-28 18:22:48

转自: http://en.cppreference.com/w/cpp/language/copy_initialization

copy initialization

Initializes an object from another object

Syntax

 
T object other ; (1)  
 
f(other); (2)  
 
return other; (3)  
 
catch ( T other) ; (4)  
 
T array [ N ] = { other }; (5)  
 

Explanation

Copy initialization is performed in the following situations:

1) when a named variable (automatic, static, or thread-local) is declared with the initializer consisting of an equals sign followed by an expression.
2) when passing an argument to a function by value
3) when returning from a function that returns by value
4) when catching an exception by value
5) as part of aggregate initialization, to initialize each element for which an initializer is provided

The effects of copy initialization are:

  • If T is a class type and the type of other is cv-unqualified version of T or a class derived from T, the constructors of Tare examined and the best match is selected by overload resolution. The constructor is then called to initialize the object.
  • If T is a class type, and the type of other is different, or if T is non-class type, but the type of other is a class type,user-defined conversion sequences that can convert from the type of other to T are examined and the best one is selected through overload resolution. The result of the conversion, which is a prvalue temporary of the destination type, is then used to direct-initialize the object. The last step is usually eliminated and the result of the conversion function is constructed directly in the memory allocated for the target object, but the copy constructor is required to be accessible even though it's not used.
  • Otherwise (if neither T nor the type of other are class types), standard conversions are used, if necessary, to convert the value of other to the cv-unqualified version of T.

Notes

Copy-initialization is less permissive than direct-initialization: copy-initialization only considers non-explicit constructors and user-defined conversion functions.

If other is an rvalue expression, move constructor will be selected by overload resolution and called during copy-initialization.

Implicit conversion is defined in terms of copy-initialization: if an object of type T can be copy-initialized with expressionE, then E is implicitly convertible to T.

The equals sign, =, in copy-initialization of a named variable is not related to the assignment operator. Assignment operator overloads have no effect on copy-initialization.

 #include <string>
#include <utility>
#include <memory> int main()
{
std::string s = "test"; // OK: constructor is non-explicit
std::string s2 = std::move(s); // this copy-initialization performs a move // std::unique_ptr<int> p = new int(1); // error: constructor is explicit
std::unique_ptr<int> p(new int()); // OK: direct-initialization int n = 3.14; // floating-integral conversion
const int b = n; // const doesn't matter
int c = b; // ...either way
}

See also

转:copy initialization的更多相关文章

  1. C&plus;&plus; 之 Direct and Copy Forms of Initialization

    Extraction from C++ Primer 5th. Editioin 3.2.1 C++ has several different forms of initialization, we ...

  2. C&plus;&plus;-copy constructor、copy-assignment operator、destructor

    本文由@呆代待殆原创,转载请注明出处. 对于一个类来说,我们把copy constructor.copy-assignment operator.move constructor.move-assig ...

  3. &lbrack;C&plus;&plus;&rsqb; Copy Control &lpar;part 1&rpar;

    Copy, Assign, and Destroy When we define a class, we specify what happens when objects of the class ...

  4. Copy elision in C&plus;&plus;

    Copy elision (or Copy omission) is a compiler optimization technique that avoids unnecessary copying ...

  5. C&plus;&plus; Primer 随笔 Chapter 13 复制控制

    1.复制控制包含的内容:复制构造函数.赋值操作符.析构函数 2.复制构造函数: a. 定义:只有单个形参,而且该形参是对本类类型的引用,这样的构造函数被成为复制构造函数 b. 适用情况: (1)根据一 ...

  6. &lbrack;C&plus;&plus;&rsqb; String Basic

    Namespace Declarations A using declaration let us use a name from a namespace without qualify the na ...

  7. 关于c&plus;&plus;的string的operator &equals;

    在 c++ primer 5 中在说到string的章节里面有这样一句话: string s5 = "hiya"; // copy initialization 也就是说,这里说上 ...

  8. complexType

    //decltype的表达式如果是加上括号的变量,结果将是引用 decltype((variable)) ruiy; //此变量的数据类型是引用(但此处变量的申明语句是错误的,引用不是对象,指向的对象 ...

  9. &lt&semi;&lt&semi;C&plus;&plus; Primer&gt&semi;&gt&semi; 第三章 字符串&comma; 向量和数组 术语表

    术语表 第 3 章 字符串, 向量和数组 begin: 是 string 和 vector 的成员,返回指向第一个元素的迭代器.也是一个标准库函数,输入一个数字,返回指向该数字首元素的指针.    缓 ...

随机推荐

  1. 360浏览器导出Excel闪退BUG

    最近这半个月在疯狂的修改各种BUG,所以比较少更新博客. 现在谈谈这个360浏览器导出Excel的BUG的解决方法. 该BUG常出现在win7系统与xp系统导出Excel的瞬间关闭导出弹窗. 目前互联 ...

  2. HD 1003 Max Sum 的递归解法

    #include <STDIO.H> typedef struct SU_tag{ SU_tag(){} SU_tag(int a,int b,int c):max_sum(a),left ...

  3. MySql对空间数据库的支持

    地址: MySQL5.1中文在线API:http://doc.mysql.cn/mysql5/refman-5.1-zh.html-chapter/spatial-extensions-in-mysq ...

  4. yii学习随感

    最近我们公司信易网络在用yii开发一个项目自己临时学习了一下yii 把学习感悟和大家一起分享一下 Yii Framework是一个基于组件.用于开发大型 Web 应用的高性能 PHP 框架.Yii提供 ...

  5. &lbrack;转&rsqb; HTC&colon;html组件

    ~~~不时会看到 用htc实现 hover, border-raius等效果,可以用来弥补IE6-8的不足.那么htc到底是什么呢? 1.摘要 本文在实例的基础上讨论了HTC(HTML Compone ...

  6. deque &lpar;STL&rpar;

    //双端队列 //deque的成员函数 c.assign(beg, end); //将[beg, end]区间中的数据赋值给c c.assign(n, elem); //将n个elem的拷贝赋值给c ...

  7. &lbrack;UWP&rsqb;如何使用Fluent Design System &lpar;下&rpar;

    4. 兼容旧版本 FDS最常见的问题之一是如何与Fall Creators Update之前的版本兼容,其实做起来也挺简单的,ColorfulBox就实现了Creators Update与Fall C ...

  8. 如何打开&period;lxe文件

    介绍两款播放器: 第一款:PotPlayer,这款软件快进看学习视频特特别方便. 软件的下载地址:链接:http://potplayer.daum.net/?lang=zh_CN 第二款:屏幕录像专家 ...

  9. sessions

    php session 用于存储有关用户回话的相关信息,或更改用户会话的相关设置,session变量保存的信息是单一用户的,并且可供应用程序中所有页面使用 session 的工作机制:为每个访问者创建 ...

  10. 从字节码的角度看Java内部类与外部类的互相访问

    Java中non-static内部类为何可以访问外部类的变量?Java中外部类又为何可以访问内部类的private变量?这两个问题困扰过我一段时间,查了一些网上的答案,大多从“闭包”概念入手,理解起来 ...