移动类的构造函数

时间:2017-11-22 00:39:11

标签: c++ c++11 move-semantics move-constructor movable

我写过两个课程,但是我把它们放在一个向量中有问题:

#include <vector>
#include <iostream>
#include <fstream>

class A
{
public:
    std::ofstream filestream;
    int aa1;
    int aa2;
    int aa3;
    int aa4;
    int aa5;
    int aa6;
    int aa7;
    int aa8;
    int aa9;
    int aa10;

    A() {}
    ~A() {}
};

class B
{
    A aaa;
public:
    int b1;
    int b2;
    int b3;
    int b4;
    int b5;
    int b6;
    int b7;
    int b8;
    int b9;
    int b10;

    B() {}
    ~B() {}
};

int main()
{
    std::vector<B> vec;
    vec.emplace_back();
    return 0;
}

输出讯息:

g++ -std=c++11 main.cpp



In file included from /usr/include/c++/5/vector:62:0,
                 from main.cpp:1:
/usr/include/c++/5/bits/stl_construct.h: In instantiation of ‘void std::_Construct(_T1*, _Args&& ...) [with _T1 = B; _Args = {B}]’:
/usr/include/c++/5/bits/stl_uninitialized.h:75:18:   required from ‘static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::move_iterator<B*>; _ForwardIterator = B*; bool _TrivialValueTypes = false]’
/usr/include/c++/5/bits/stl_uninitialized.h:126:15:   required from ‘_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::move_iterator<B*>; _ForwardIterator = B*]’
/usr/include/c++/5/bits/stl_uninitialized.h:281:37:   required from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = std::move_iterator<B*>; _ForwardIterator = B*; _Tp = B]’
/usr/include/c++/5/bits/stl_uninitialized.h:303:2:   required from ‘_ForwardIterator std::__uninitialized_move_if_noexcept_a(_InputIterator, _InputIterator, _ForwardIterator, _Allocator&) [with _InputIterator = B*; _ForwardIterator = B*; _Allocator = std::allocator<B>]’
/usr/include/c++/5/bits/vector.tcc:422:8:   required from ‘void std::vector<_Tp, _Alloc>::_M_emplace_back_aux(_Args&& ...) [with _Args = {}; _Tp = B; _Alloc = std::allocator<B>]’
/usr/include/c++/5/bits/vector.tcc:101:23:   required from ‘void std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {}; _Tp = B; _Alloc = std::allocator<B>]’
main.cpp:46:22:   required from here
/usr/include/c++/5/bits/stl_construct.h:75:7: error: use of deleted function ‘B::B(const B&)’
     { ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
       ^
main.cpp:24:7: note: ‘B::B(const B&)’ is implicitly deleted because the default definition would be ill-formed:
 class B
       ^
main.cpp:24:7: error: use of deleted function ‘A::A(const A&)’
main.cpp:5:7: note: ‘A::A(const A&)’ is implicitly deleted because the default definition would be ill-formed:
 class A
       ^
main.cpp:5:7: error: use of deleted function ‘std::basic_ofstream<_CharT, _Traits>::basic_ofstream(const std::basic_ofstream<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits<char>]’
In file included from main.cpp:3:0:
/usr/include/c++/5/fstream:723:7: note: declared here
       basic_ofstream(const basic_ofstream&) = delete;
       ^

警告和错误不言自明。但我在移动一些课程方面是新手。我该如何正确移动ofstream?如果我定义move constructor,我是否应该将所有单个元素从aa1移到aa10?有什么方法可以解决它?

根据其他posts,必须可以为我的g++版本移动.stream。为什么它没有隐含地移动?

g++ -v
 ...
Thread model: posix
gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.5) 

2 个答案:

答案 0 :(得分:3)

  

为什么它没有隐含地移动?

当提供用户定义的析构函数时,移动构造函数不会是implicitly declared

  
      
  • 没有用户声明的副本构造函数;
  •   
  • 没有用户声明的副本赋值运算符;
  •   
  • 没有用户声明的移动赋值运算符;
  •   
  • 没有用户声明的析构函数;
  •   

您可以删除AB的析构函数声明,让编译器为您生成移动构造函数,或者您可以使用default显式定义它们。对于这两种情况,移动构造函数执行对象基础和非静态成员的完全成员移动。 e.g。

class A
{
public:
    ...
    A(A&&) = default;
};

class B
{
public:
    ...
    B(B&&) = default;
};

LIVE

答案 1 :(得分:1)

添加到songyuanyao回答的内容,这个post解释了关于隐式构造函数的所有内容。