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

标签 c++ gcc c++14 move-semantics

以下代码无法编译 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/

相关文章:

C++ 风格 : Prefixing virtual keyword to overridden methods

c++ - 多种字体大小的 FreeType 字形指标缓存

c++ - 休眠失败时的 Windows 通知

c++ - 为什么我们需要抽象类而不是虚拟类?

gcc - NVCC 单独编译,带 PTX 输出

gcc - 将 GCC 编译器安装到 Docker 容器上

c++ - 在实现文件中使用 typedef 来缩短类型签名的负面后果是什么?

c++ - 这是以前做过的吗? (用于链式操作的 Monad View 包装 C++ 集合/类型)

c++ - 在父容器中调用基于子容器的重载函数

c - 如何在c中对主文件的功能进行单元测试