class 的移动构造函数
Move constructor for a class
我已经写了两个 类 但我在将它们放入向量中时遇到了问题:
#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
?有什么办法绕过它吗?
根据其他 的说法,ofstream 对于我的 g++
版本必须是可移动的。为什么它不隐式移动?
g++ -v
...
Thread model: posix
gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.5)
Why doesn't it move implicitly?
如果提供了用户定义的析构函数,则移动构造函数不会implicitly declared:
- there are no user-declared copy constructors;
- there are no user-declared copy assignment operators;
- there are no user-declared move assignment operators;
- there are no user-declared destructors;
您可以删除 A
和 B
的析构函数声明,让编译器为您生成移动构造函数,或者您可以使用 default
显式定义它们。对于这两种情况,移动构造函数都对对象的基类和非静态成员执行完整的成员智能移动。例如
class A
{
public:
...
A(A&&) = default;
};
class B
{
public:
...
B(B&&) = default;
};
加上 songyuanyao 的回答,post 完美解释了隐式构造函数的一切。
我已经写了两个 类 但我在将它们放入向量中时遇到了问题:
#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
?有什么办法绕过它吗?
根据其他 g++
版本必须是可移动的。为什么它不隐式移动?
g++ -v
...
Thread model: posix
gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.5)
Why doesn't it move implicitly?
如果提供了用户定义的析构函数,则移动构造函数不会implicitly declared:
- there are no user-declared copy constructors;
- there are no user-declared copy assignment operators;
- there are no user-declared move assignment operators;
- there are no user-declared destructors;
您可以删除 A
和 B
的析构函数声明,让编译器为您生成移动构造函数,或者您可以使用 default
显式定义它们。对于这两种情况,移动构造函数都对对象的基类和非静态成员执行完整的成员智能移动。例如
class A
{
public:
...
A(A&&) = default;
};
class B
{
public:
...
B(B&&) = default;
};
加上 songyuanyao 的回答,post 完美解释了隐式构造函数的一切。