转自: 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++ 之 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++-copy constructor、copy-assignment operator、destructor

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

  3. [C++] Copy Control (part 1)

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

  4. Copy elision in C++

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

  5. C++ Primer 随笔 Chapter 13 复制控制

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

  6. [C++] String Basic

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

  7. 关于c++的string的operator =

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

  8. complexType

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

  9. <<C++ Primer>> 第三章 字符串, 向量和数组 术语表

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

随机推荐

  1. Unity-Animator深入系列---控制IK

    回到 Animator深入系列总目录 要让代码控制IK,需要先在Animator中打开IK pass 然后,和IK相关的代码需要放到相应的函数中去: void OnAnimatorIK() { Deb ...

  2. CodeBlocks养眼的colour theme

    把如下的代码复制粘贴到这个文件:default.conf <?xml version="1.0" encoding="UTF-8" standalone= ...

  3. 公共控件Listview

    ListView属性中,Items是行的总集合,Items集合中的每一个是一行,Items集合里面有ListViewItem集合,这个集合实例化:ListViewItem li=new ListVie ...

  4. 【leetcode❤python】235. Lowest Common Ancestor of a Binary Search Tree

    #-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):#     def __init ...

  5. 【leetcode❤python】119. Pascal's Triangle II

    #-*- coding: UTF-8 -*-#杨辉三角返回给定行#方法:自上而下考虑问题,从给定的一行计算出下一行,则给定行数之后,计算的最后一行就是求解的最后一行class Solution(obj ...

  6. 解决HtmlAgilityPack无法获取form标签子节点的问题

    问题描述 今天使用HtmlAgilityPack提取Form表单下的input节点,发现提取的form节点没有子节点,InnerHtml也是为空,起初以为是标签不全导致,后来分析html代码发现不可能 ...

  7. window删除损坏无法打开的文件

    移动硬盘删除文件时提示“文件或目录损坏且无法读取”的解决方法-chkdsk 命令的巧用 新买一个移动硬盘,同学借去Copy一个游戏,拷来后发现数据包损坏,提示"文件或目录损坏且无法读取&qu ...

  8. Upgrade R (升级R语言)

    R R version 3.1.1 (2014-07-10) -- "Sock it to Me" yum list installed | grep R R-core.x86_6 ...

  9. SpringMVC 配置过滤器解决中文乱码问题

    <!-- 字符集过滤器 -->      <filter>          <filter-name>Charset</filter-name>   ...

  10. CUBRID学习笔记 29 web管理中文语言文件 CUBRID教程

    网站的中文语言文件部分 http://files.cnblogs.com/files/wang2650/Messages.7z