C++错误:在移动构造函数中使用已删除的函数

C++ error: Use of deleted function in move constructor

本文关键字:删除 函数 错误 移动 构造函数 C++      更新时间:2023-10-16
#include<iostream>
#include<vector>
class Container {
    int * m_Data;
public:
    Container() {
        //Allocate an array of 20 int on heap
        m_Data = new int[20];
        std::cout << "Constructor: Allocation 20 int" << std::endl;
    }
    ~Container() {
        if (m_Data) {
            delete[] m_Data;
            m_Data = NULL;
        }
        std::cout << "Destructor called" << std::endl;
    }
    Container(const Container & obj) {
        //Allocate an array of 20 int on heap
        m_Data = new int[20];
        //Copy the data from passed object
        for (int i = 0; i < 20; i++)
            m_Data[i] = obj.m_Data[i];
        std::cout << "Copy Constructor: Allocation 20 int" << std::endl;
    }
    // will give error on adding
    Container(Container&&) = delete;
};
// Create am object of Container and return
Container getContainer() 
{
    Container obj;
    return obj;
}
int main() {
    // Create a vector of Container Type
    std::vector<Container> vecOfContainers;
    //Add object returned by function into the vector
    vecOfContainers.push_back(getContainer());
    return 0;
}

我已经编写了上面的代码,作为实践来验证从 getContainer(( 返回对象时是否调用复制构造函数,即使它是一个右值。

我知道如果用户定义的复制构造函数存在,那么编译器不会将移动构造函数声明为其类的非显式内联公共成员,签名为 T::T(T&&(。我仍然决定添加行"容器(容器&&(=删除;",并在编译过程中收到以下错误:

moveconstructorIntro.cpp: In function ‘Container getContainer()’:
moveconstructorIntro.cpp:44:12: error: use of deleted function 
‘Container::Container(Container&&)’
     return obj;
            ^
moveconstructorIntro.cpp:36:5: error: declared here
     Container(Container&&) = delete;
     ^
In file included from /usr/include/c++/4.8.2/x86_64-redhat-
linux/bits/c++allocator.h:33:0,
                 from /usr/include/c++/4.8.2/bits/allocator.h:46,
                 from /usr/include/c++/4.8.2/string:41,
                 from /usr/include/c++/4.8.2/bits/locale_classes.h:40,
                 from /usr/include/c++/4.8.2/bits/ios_base.h:41,
                 from /usr/include/c++/4.8.2/ios:42,
                 from /usr/include/c++/4.8.2/ostream:38,
                 from /usr/include/c++/4.8.2/iostream:39,
                 from moveconstructorIntro.cpp:5:
/usr/include/c++/4.8.2/ext/new_allocator.h: In instantiation of ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = Container; _Args = {Container}; _Tp = Container]’:
/usr/include/c++/4.8.2/bits/alloc_traits.h:254:4:   required from ‘static typename std::enable_if<std::allocator_traits<_Alloc>::__construct_helper<_Tp, _Args>::value, void>::type std::allocator_traits<_Alloc>::_S_construct(_Alloc&, _Tp*, _Args&& ...) [with _Tp = Container; _Args = {Container}; _Alloc = std::allocator<Container>; typename std::enable_if<std::allocator_traits<_Alloc>::__construct_helper<_Tp, _Args>::value, void>::type = void]’
/usr/include/c++/4.8.2/bits/alloc_traits.h:393:57:   required from ‘static decltype (_S_construct(__a, __p, (forward<_Args>(std::allocator_traits::construct::__args)...)) std::allocator_traits<_Alloc>::construct(_Alloc&, _Tp*, _Args&& ...) [with _Tp = Container; _Args = {Container}; _Alloc = std::allocator<Container>; decltype (_S_construct(__a, __p, (forward<_Args>(std::allocator_traits::construct::__args)...)) = <type error>]’
/usr/include/c++/4.8.2/bits/vector.tcc:97:40:   required from ‘void std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {Container}; _Tp = Container; _Alloc = std::allocator<Container>]’
/usr/include/c++/4.8.2/bits/stl_vector.h:920:36:   required from ‘void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = Container; _Alloc = std::allocator<Container>; std::vector<_Tp, _Alloc>::value_type = Container]’
moveconstructorIntro.cpp:52:45:   required from here
/usr/include/c++/4.8.2/ext/new_allocator.h:120:4: error: use of deleted 
function ‘Container::Container(Container&&)’
  { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
    ^
moveconstructorIntro.cpp:36:5: error: declared here
     Container(Container&&) = delete;
     ^

错误的原因是什么?

请不要试图为我在这里做的事情找到任何用例。我刚开始用C++编写代码并试图掌握。

显式删除移动构造函数时,已将其用于重载解析。

当按值返回本地参数时,移动构造函数被选为更好的重载候选(而不是复制构造函数(,但在删除时无法使用。

请记住,不声明移动构造函数并显式删除它会在重载解决过程中产生不同的结果!

编辑

虽然我上面所说的是正确的,但碰巧的是,编译器在此代码中报告的特定报告位于另一个区域,即当值被推回容器时发生的移动尝试。那里也会发生同样的事情 - 在重载解析期间选择了移动构造函数,但在删除时无法使用。