简体   繁体   English

为什么使用“无 const 复制构造函数”时编译器会报错?

[英]Why does the compiler complains when `none const copy constructor` is used?

As the subject, the code below is right.作为主体,下面的代码是对的。

#include<iostream>

class ABC     
{  public:  
    ABC() 
    {
        std::cout<< "default construction" << std::endl;
    }

    ABC(ABC& a) 
    {
        std::cout << "copy construction" << std::endl;
    } 

};                         

int main()   
{  
   ABC c1 = ABC(); 
}

It could not compile successfully:它无法成功编译:

<source>: In function 'int main()': 
<source>:25:13: error: cannot bind non-const lvalue reference of type 'ABC&' to an rvalue of type 'ABC'
   25 |    ABC c1 = ABC();
      |             ^~~~~
<source>:10:14: note:   initializing argument 1 of 'ABC::ABC(ABC&)'
   10 |     ABC(ABC& a)
      |         ~~~~~^

However, it could compile if replace the ABC(ABC& a) by ABC(const ABC&) .I know it has some relation with the keyword const .But i could not figure out why.但是,如果将ABC(ABC& a)替换为 ABC ABC(const ABC&) ,它可以编译。我知道它与关键字const有一些关系。但我不知道为什么。

You could check it on https://godbolt.org/z/jNL5Bd .您可以在https://godbolt.org/z/jNL5Bd上查看它。 I am a novice in C++.I would be grateful to have some help with this question.我是 C++ 的新手。如果能在这个问题上得到一些帮助,我将不胜感激。

As the error message said, the temporary ABC() can't be bound to lvalue-reference to non-const, the copy constructor taking ABC& can't be used for initialization.正如错误消息所说,临时ABC()不能绑定到非 const 的左值引用,采用ABC&的复制构造函数不能用于初始化。 (Temporaries could be bound to lvalue-reference to const or rvalue-reference.) (临时对象可以绑定到 const 或 rvalue-reference 的左值引用。)


PS: Since C++17 the code would compile (which doesn't mean the copy constructor taking lvalue-reference to non-const is a good manner), because copy elision is guaranteed, the copy construction would be elided completely. PS:由于C++17代码可以编译(这并不意味着复制构造函数对非 const 进行左值引用是一种好方法),因为保证了复制省略,所以复制构造将被完全省略。

(emphasis mine) (强调我的)

Under the following circumstances, the compilers are required to omit the copy and move construction of class objects, even if the copy/move constructor and the destructor have observable side-effects .在以下情况下,编译器需要省略 class 对象的复制和移动构造,即使复制/移动构造函数和析构函数具有可观察到的副作用 The objects are constructed directly into the storage where they would otherwise be copied/moved to.对象直接构建到存储中,否则它们将被复制/移动到。 The copy/move constructors need not be present or accessible :复制/移动构造函数不需要存在或可访问

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么当我声明并定义不带参数的构造函数和带参数的构造函数时,编译器会抱怨? - Why does the compiler complains when I declare and define a constructor with no argument and a constructor with an argument? 为什么编译器在分配时调用模板化副本构造函数? - Why does the compiler invoke a templated copy constructor when assigning? 复制构造函数未调用,但编译器抱怨没有 - Copy constructor not called, but compiler complains that there's no 为什么编译器使用move构造函数而不是copy构造函数 - Why does compiler use move constructor instead of the copy constructor 为什么在const对象上调用std :: move会在传递给另一个对象时调用复制构造函数? - Why does calling std::move on a const object call the copy constructor when passed to another object? 在返回作为const引用的字符串时,编译器是否避免使用副本? - Does the compiler avoid a copy when returning a string that is taken as const reference? 为什么复制构造函数在通过const引用传递临时时被调用? - why copy constructor is called when passing temporary by const reference? 为什么即使参数标记为“const”,也会调用复制构造函数? - Why is copy constructor called even when parameter is marked 'const'? 编译器生成的复制构造函数/赋值是否使用const / volatile呈现它的参数 - Does compiler generated copy constructor/assignment renders it's parameter with const/volatile 编译器何时在C ++中创建复制构造函数? - When does the compiler creates the copy-constructor in C++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM