【问题标题】:move semantics for different qt versions移动不同 qt 版本的语义
【发布时间】:2017-07-26 11:32:08
【问题描述】:

我是移动语义的初学者,我对编译结果感到困惑。当我使用 qt 5.5.1 时一切正常,但是当我使用 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 似乎不支持某些东西,我应该考虑到这一点并更改我的代码。但是不知道有什么变化。 提前谢谢你。

【问题讨论】:

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


    【解决方案1】:

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

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

    这很有帮助,因为移动构造函数是在 Qt 5.2 中引入的。

    【讨论】:

    • 理想情况下,首选初始化列表:TChannel(TChannel &amp;&amp; that) : _title(std::move(that._title)) {}.
    猜你喜欢
    • 1970-01-01
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-29
    • 1970-01-01
    • 2016-04-14
    相关资源
    最近更新 更多