移动不同qt版本的语义

时间:2017-03-06 14:36:11

标签: qt c++11 smart-pointers move-semantics

我是移动语义的初学者,我对编译结果感到困惑。当我使用qt 5.5.1时,所有都是okey,但是当我使用qt 4.8.7时,我有构建错误。我正在尝试构建以下代码:

struct TResource
{
   int _resource_id = 18;
};
typedef std::vector<std::unique_ptr<TResource>> TResources;

struct TChannel
{
   QString _title;
   TResources _ress;
};
typedef std::vector<TChannel> TChanneles;


int main(int , char **)
{
   TChannel channel;
   TChanneles chnls;

   //Creating some channel resorces and place it to the vector
   // .......
   chnls.push_back(std::move(channel));

   return 0;
}

错误消息如下:

g++ -c -m64 -pipe -std=c++0x -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++-64 -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4 -I. -o main.o main.cpp
In file included from /usr/include/c++/5/bits/stl_tempbuf.h:60:0,
                 from /usr/include/c++/5/bits/stl_algo.h:62,
                 from /usr/include/c++/5/algorithm:62,
                 from /usr/include/qt4/QtCore/qglobal.h:68,
                 from /usr/include/qt4/QtCore/qnamespace.h:45,
                 from /usr/include/qt4/QtCore/qobjectdefs.h:45,
                 from /usr/include/qt4/QtCore/qobject.h:47,
                 from /usr/include/qt4/QtCore/qcoreapplication.h:45,
                 from /usr/include/qt4/QtCore/QCoreApplication:1,
                 from main.cpp:1:
/usr/include/c++/5/bits/stl_construct.h: In instantiation of ‘void std::_Construct(_T1*, _Args&& ...) [with _T1 = std::unique_ptr<TResource>; _Args = {const std::unique_ptr<TResource, std::default_delete<TResource> >&}]’:
/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 = __gnu_cxx::__normal_iterator<const std::unique_ptr<TResource>*, std::vector<std::unique_ptr<TResource> > >; _ForwardIterator = std::unique_ptr<TResource>*; bool _TrivialValueTypes = false]’
/usr/include/c++/5/bits/stl_uninitialized.h:126:15:   required from ‘_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::unique_ptr<TResource>*, std::vector<std::unique_ptr<TResource> > >; _ForwardIterator = std::unique_ptr<TResource>*]’
/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 = __gnu_cxx::__normal_iterator<const std::unique_ptr<TResource>*, std::vector<std::unique_ptr<TResource> > >; _ForwardIterator = std::unique_ptr<TResource>*; _Tp = std::unique_ptr<TResource>]’
/usr/include/c++/5/bits/stl_vector.h:322:31:   required from ‘std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = std::unique_ptr<TResource>; _Alloc = std::allocator<std::unique_ptr<TResource> >]’
main.cpp:11:8:   required from ‘void std::_Construct(_T1*, _Args&& ...) [with _T1 = TChannel; _Args = {TChannel&}]’
/usr/include/c++/5/bits/stl_uninitialized.h:75:18:   [ skipping 2 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ]
/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 = TChannel*; _ForwardIterator = TChannel*; _Tp = TChannel]’
/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 = TChannel*; _ForwardIterator = TChannel*; _Allocator = std::allocator<TChannel>]’
/usr/include/c++/5/bits/vector.tcc:422:8:   required from ‘void std::vector<_Tp, _Alloc>::_M_emplace_back_aux(_Args&& ...) [with _Args = {TChannel}; _Tp = TChannel; _Alloc = std::allocator<TChannel>]’
/usr/include/c++/5/bits/vector.tcc:101:23:   required from ‘void std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {TChannel}; _Tp = TChannel; _Alloc = std::allocator<TChannel>]’
/usr/include/c++/5/bits/stl_vector.h:932:21:   required from ‘void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = TChannel; _Alloc = std::allocator<TChannel>; std::vector<_Tp, _Alloc>::value_type = TChannel]’
main.cpp:26:39:   required from here
/usr/include/c++/5/bits/stl_construct.h:75:7: error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = TResource; _Dp = std::default_delete<TResource>]’
     { ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
       ^
In file included from /usr/include/c++/5/memory:81:0,
                 from main.cpp:2:
/usr/include/c++/5/bits/unique_ptr.h:356:7: note: declared here
       unique_ptr(const unique_ptr&) = delete;
       ^
Makefile:203: recipe for target 'main.o' failed
make: *** [main.o] Error 1

似乎qt 4.8.7中不支持某些内容,我应该考虑到这一点并更改我的代码。但我不知道有什么变化。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

我已解决了我的问题。 A添加了移动构造函数TChannel struct:

TChannel(TChannel&& that)
{
    _title = std::move(that._title);
}

它有用,因为移动构造函数是在Qt 5.2中引入的。