std::unique_ptr use of deleted function с move
Всем привет, есть следующий код:
struct RenderableAtmosphere {
std::unique_ptr<Atmosphere> atmosphere;
float hScaleFactor, parentEarthSizeCoefficient;
bool isUseToneMapping = false;
};
struct RenderableSceneComponent {
...
std::vector<RenderableAtmosphere> atmospheres;
};
...
RenderableSceneComponent uranusSystemComponent;
uranusSystemComponent.atmospheres = vector<RenderableAtmosphere>{move(renderableUranusAtmosphere)};
//uranusSystemComponent.atmospheres.push_back(move(renderableUranusAtmosphere));
В случае с вызовом оператора перемещающего присваивания код не компилируется со следующим набором ошибок:
In file included from D:/MinGW/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/vector:62,
from D:\Clion\OpenGL_Projects\CourseWork_SolarSystem\src\Auxiliary_Modules/Camera.h:5,
from D:\Clion\OpenGL_Projects\CourseWork_SolarSystem\src\Auxiliary_Modules/AuxiliaryModules.h:4,
from D:\Clion\OpenGL_Projects\CourseWork_SolarSystem\src\Application.h:3,
from D:\Clion\OpenGL_Projects\CourseWork_SolarSystem\src\Application.cpp:1:
D:/MinGW/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/stl_construct.h: In instantiation of 'void std::_Construct(_T1*, _Args&& ...) [with _T1 = RenderableAtmosphere; _Args = {const RenderableAtmosphere&}]':
D:/MinGW/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/stl_uninitialized.h:83:18: required from 'static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = const RenderableAtmosphere*; _ForwardIterator = RenderableAtmosphere*; bool _TrivialValueTypes = false]'
D:/MinGW/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/stl_uninitialized.h:134:15: required from '_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = const RenderableAtmosphere*; _ForwardIterator = RenderableAtmosphere*]'
D:/MinGW/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/stl_uninitialized.h:289:37: required from '_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = const RenderableAtmosphere*; _ForwardIterator = RenderableAtmosphere*; _Tp = RenderableAtmosphere]'
D:/MinGW/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/stl_vector.h:1464:33: required from 'void std::vector<_Tp, _Alloc>::_M_range_initialize(_ForwardIterator, _ForwardIterator, std::forward_iterator_tag) [with _ForwardIterator = const RenderableAtmosphere*; _Tp = RenderableAtmosphere; _Alloc = std::allocator<RenderableAtmosphere>]'
D:/MinGW/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/stl_vector.h:519:2: required from 'std::vector<_Tp, _Alloc>::vector(std::initializer_list<_Tp>, const allocator_type&) [with _Tp = RenderableAtmosphere; _Alloc = std::allocator<RenderableAtmosphere>; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<RenderableAtmosphere>]'
D:\Clion\OpenGL_Projects\CourseWork_SolarSystem\src\Application.cpp:888:102: required from here
D:/MinGW/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/stl_construct.h:75:7: error: use of deleted function 'RenderableAtmosphere::RenderableAtmosphere(const RenderableAtmosphere&)'
{ ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
Я не могу понять, в какой момент вызывается копия класса, которая пытается вызвать удалённый конструктор копирования у unique_ptr. Но если использовать push_back, то всё работает
Ответы (1 шт):
Автор решения: Глеб Труфанов
→ Ссылка
Здесь ответили верно, фишка в initializer_list, в котором элементы хранятся как константы, и перемещение из него уже работать по этой причине не будет (из-за константности)