为什么不使用声明继承移动构造函数

问题描述 投票:1回答:2

在下面的代码中,尽管基类是可移动构造的,但显然不会生成派生类的移动构造函数。

#include <cstddef>
#include <memory>
#include <cstring>
#include <cassert>

template <typename T>
class unique_array : public std::unique_ptr<T[],void (*)(void*)>
{   size_t Size;
 protected:
    typedef std::unique_ptr<T[],void (*)(void*)> base;
    unique_array(T* ptr, size_t size, void (*deleter)(void*)) noexcept : base(ptr, deleter), Size(size) {}
 public:
    constexpr unique_array() noexcept : base(NULL, operator delete[]), Size(0) {}
    explicit unique_array(size_t size) : base(new T[size], operator delete[]), Size(size) {}
    unique_array(unique_array<T>&& r) : base(move(r)), Size(r.Size) { r.Size = 0; }
    void reset(size_t size = 0) { base::reset(size ? new T[size] : NULL); Size = size; }
    void swap(unique_array<T>&& other) noexcept { base::swap(other); std::swap(Size, other.Size); }
    size_t size() const noexcept { return Size; }
    T* begin() const noexcept { return base::get(); }
    T* end() const noexcept { return begin() + Size; }
    T& operator[](size_t i) const { assert(i < Size); return base::operator[](i); }
    unique_array<T> slice(size_t start, size_t count) const noexcept
    {   assert(start + count <= Size); return unique_array<T>(begin() + start, count, [](void*){}); }
};

template <typename T>
class unique_num_array : public unique_array<T>
{   static_assert(std::is_arithmetic<T>::value, "T must be arithmetic");
 public:
    using unique_array<T>::unique_array;
    unique_num_array(unique_num_array<T>&& r) : unique_array<T>(move(r)) {}
    unique_num_array<T> slice(size_t start, size_t count) const noexcept
    {   assert(start + count <= this->size()); return unique_num_array<T>(this->begin() + start, count, [](void*){}); }
 public: // math operations
    void clear() const { std::memset(this->begin(), 0, this->size() * sizeof(T)); }
    const unique_num_array<T>& operator =(const unique_num_array<T>& r) const { assert(this->size() == r.size()); memcpy(this->begin(), r.begin(), this->size() * sizeof(T)); return *this; }
    const unique_num_array<T>& operator +=(const unique_num_array<T>& r) const;
    // ...
};

int main()
{   // works
    unique_array<int> array1(7);
    unique_array<int> part1 = array1.slice(1,3);
    // does not work
    unique_num_array<int> array2(7);
    unique_num_array<int> part2 = array2.slice(1,3);
    // test for default constructor
    unique_num_array<int> array3;
    return 0;
}

使用上面的代码我得到一个错误(gcc 4.8.4):

test6.cpp:在函数'int main()'中:test6.cpp:47:48:错误:使用已删除的函数'unique_num_array :: unique_num_array(const unique_num_array&)'unique_num_array part2 = array2.slice(1,3);

派生类中的切片函数不能按值返回,因为缺少移动构造函数。所有其他构造函数似乎按预期工作(本示例未涵盖)。

如果我显式定义移动构造函数(取消注释行),则示例编译。但在这种情况下,默认构造函数会消失,这当然不是预期的。

这里发生了什么?我不明白其中任何一个案例。

为什么在第一种情况下删除了移动构造函数?

为什么在第二种情况下删除了默认构造函数?其他人似乎生存下来。

c++ c++11 constructor using-declaration
2个回答
4
投票

为什么在第一种情况下删除了移动构造函数?

因为unique_num_array<T>中有一个用户声明的复制赋值运算符,所以编译器不会隐式声明移动构造函数。 [class.copy.ctor] / 8中的标准说

如果类X的定义没有显式声明一个移动构造函数,那么当且仅当一个非显式的构造函数被隐式声明为默认的

  • X没有用户声明的复制构造函数,
  • X没有用户声明的复制赋值运算符,
  • X没有用户声明的移动赋值运算符,和
  • X没有用户声明的析构函数。

为什么在第二种情况下删除了默认构造函数?

因为在unique_num_array<T>中有一个用户声明的移动构造函数,所以编译器不会隐式声明默认构造函数。 [class.ctor] / 4中的标准说

...如果类X没有用户声明的构造函数,则不会将没有参数的非显式构造函数隐式声明为默认值([dcl.fct.def])。


此外,由于保证copy elision,此代码将在C ++ 17之后工作。详细地说,在C ++ 17之前,这两个语境都是语境

return unique_num_array<T>(...);

unique_num_array<int> part2 = array2.slice(1,3);

需要复制/移动操作,而在C ++ 17之后,语义变为目标对象由prvalue初始化程序初始化而不会实现临时,因此不需要复制/移动。


1
投票

这里适用两套规则:

  1. using指令不包含move构造函数和默认构造函数。 [...]所有候选继承的构造函数都不是默认构造函数或复制/移动构造函数,并且其签名与派生类中的用户定义构造函数不匹配,在派生类中隐式声明。
  2. 现在应用自动生成非显式构造函数的规则(如xskxsr已经提到的)。 如果类X的定义没有显式声明一个移动构造函数,那么当且仅当[...] X没有用户声明的复制赋值运算符时,非显式的一个将被隐式声明为默认值。 [...]如果类X没有用户声明的构造函数,则没有参数的非显式构造函数被隐式声明为默认值([dcl.fct.def])。
© www.soinside.com 2019 - 2024. All rights reserved.