gpt4 book ai didi

c++ - 在继承的构造函数中仅 move 类参数

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

以下代码无法编译 GCC 6.1 , 但适用于 Clang 3.8.0Visual Studio 2015 :

#include <memory>

class base {
public:
base(std::unique_ptr<int>) {}
};

class derived : public base {
public:
using base::base;
};

int main() {
derived df(std::make_unique<int>());
}

出现以下错误:

main.cpp: In constructor 'derived::derived(std::unique_ptr<int>)':

main.cpp:10:17: error: use of deleted function
'std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&)
[with _Tp = int; _Dp = std::default_delete<int>]'

using base::base;

^~~~

In file included from /usr/local/include/c++/6.1.0/memory:81:0,

from main.cpp:1:

/usr/local/include/c++/6.1.0/bits/unique_ptr.h:356:7: note: declared here

unique_ptr(const unique_ptr&) = delete;

^~~~~~~~~~

main.cpp: In function 'int main()':

main.cpp:14:39: note: synthesized method 'derived::derived(std::unique_ptr<int>)'
first required here

derived df(std::make_unique<int>());

它似乎在尝试调用已删除的复制构造函数,但这工作得很好:

void foo(std::unique_ptr<int>) {}

int main() {
foo(std::make_unique<int>());
}

这个带有 -fno-elide-constructors 的例子打印出 move called.:

struct move_only {
move_only() { std::cout << "default called."; }
move_only(move_only&&) { std::cout << "move called."; }
};

void foo(move_only) { }

int main() {
foo(move_only{});
}

我意识到这两种情况并不相同,但看起来很奇怪 && 是使继承的构造函数示例编译而不是后者所必需的。作为健全性检查,显式执行 move_only(const move_only&) = delete; 并将签名更改为 void foo(const move_only&) { } 仍然可以编译,除了这次 move甚至没有调用构造函数(可能省略)。

12.6.3最新的标准草案说:

1 When a constructor for type B is invoked to initialize an object of a different type D (that is, when the constructor was inherited ([namespace.udecl])), initialization proceeds as if a defaulted default constructor were used to initialize the D object and each base class subobject from which the constructor was inherited, except that the B subobject is initialized by the invocation of the inherited constructor. The complete initialization is considered to be a single function call; in particular, the initialization of the inherited constructor's parameters is sequenced before the initialization of any part of the D object. [ Example:

struct B1 {
B1(int, ...) { }
};

// ...

struct D1 : B1 {
using B1::B1; // inherits B1(int, ...)
int x;
// ...
};

void test() {
D1 d(2, 3, 4); // OK: B1 is initialized by calling B1(2, 3, 4),
// then d.x is default-initialized (no initialization is performed),
// ...
}

// ...

所以它应该完全等同于 foo(move_only) 对吧?

最佳答案

这似乎是一个错误(报告为 bug 70972)。 N4140 [class.inhctor]/8:

An implicitly-defined inheriting constructor performs the set of initializations of the class that would be performed by a user-written inline constructor for that class with a mem-initializer-list whose only mem-initializer has a mem-initializer-id that names the base class denoted in the nested-name-specifier of the using-declaration and an expression-list as specified below, and where the compound-statement in its function body is empty (12.6.2). If that user-written constructor would be ill-formed, the program is ill-formed. Each expression in the expression-list is of the form static_cast<T&&>(p), where p is the name of the corresponding constructor parameter and T is the declared type of p.

换句话说,这里讨论的继承构造函数应该是 move 而不是复制它的参数。

关于c++ - 在继承的构造函数中仅 move 类参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37064993/

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