gpt4 book ai didi

c++ - 显式移动构造函数是否消除了隐式复制构造函数?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:23:45 25 4
gpt4 key购买 nike

我阅读了接受的答案 here那:

[a] copy constructor and copy assignment operator won't be generated for a class that explicitly declares a move constructor or move assignment operator

我确实注意到 (g++ 4.7.2) 如果您定义了一个移动构造函数,它将与例如 push_back() 一起使用,而如果您所做的只是 = delete 复制构造函数,你没有得到隐式移动构造函数——你得到一个错误。 [...这让我想知道如果您没有明确地做任何事情,实际使用的是哪一个(移动或复制)...]

然而,this online reference当您定义移动构造函数时,不会对复制构造函数做出相同的明确 promise 被隐式定义。

所以我的问题是,第一个报价是否由标准(包括“或”)保证?对于一些需要显式析构函数的类,我更愿意仅使用一个移动构造函数和一个(已删除的)移动运算符来完成“五规则”,并依赖于定义的隐式复制方法.如果我不能依赖它,那么我将不得不显式地 =delete 它们——但那是很多潜在冗余的东西。

最佳答案

So my question is, is the first quote guaranteed by the standard (including the "or")?

是的,您的第一个报价由标准保证。

引自标准(草案 n3690):

12.8 Copying and moving class objects [class.copy]

7/ If the class definition does not explicitly declare a copy constructor, one is declared implicitly. If the class definition declares a move constructor or move assignment operator, the implicitly declared copy constructor is defined as deleted; otherwise, it is defined as defaulted (8.4). The latter case is deprecated if the class has a user-declared copy assignment operator or a user-declared destructor.

关于c++ - 显式移动构造函数是否消除了隐式复制构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18895784/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com