#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
class Base {
public:
virtual ~Base() = default;
virtual void doIt() = 0;
public:
int base = 1;
};
class Derived : public Base{
public:
Derived(int x) : derived(x) { base = x; }
void doIt() override { printf("hello from derived class. %d, %d\n", derived, base); }
public:
int derived = 2;
};
class Storage {
public:
std::map<std::string, std::vector<std::vector<std::unique_ptr<Base>>>> cache;
void add_to_cache(std::unique_ptr<Base> ptr) {
cache["row"].back().push_back(std::move(ptr));
}
void add_new_cacheLine() {
cache.emplace("row", std::vector<std::vector<std::unique_ptr<Base>>>{}); // Case I: compile ok.
/*
std::vector<std::vector<std::unique_ptr<Base>>> emptyCacheLine{};
cache.insert("row", emptyCacheLine); // Case II: compile error
*/
}
Storage() {
cache["row"] = {};
}
};
int main()
{
Storage storage;
storage.add_to_cache(std::make_unique<Derived>(3));
for (auto& s : storage.cache["row"].back()) {
s->doIt();
}
}
Here is my example code, when compile it with MSVC with Case II, I get:
Error C2280 'std::unique_ptr<Base,std::default_delete<Base>> &std::unique_ptr<Base,std::default_delete<Base>>::operator =(const std::unique_ptr<Base,std::default_delete<Base>> &)': attempting to reference a deleted function
The full build logs here:
Build started...
1>------ Build started: Project: ConsoleApplication1, Configuration: Debug x64 ------
1>ConsoleApplication1.cpp
1>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.36.32532\include\xmemory(677,47): error C2280: 'std::unique_ptr<Base,std::default_delete<Base>>::unique_ptr(const std::unique_ptr<Base,std::default_delete<Base>> &)': attempting to reference a deleted function
1>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.36.32532\include\memory(3317,5): message : see declaration of 'std::unique_ptr<Base,std::default_delete<Base>>::unique_ptr'
1>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.36.32532\include\memory(3317,5): message : 'std::unique_ptr<Base,std::default_delete<Base>>::unique_ptr(const std::unique_ptr<Base,std::default_delete<Base>> &)': function was explicitly deleted
1>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.36.32532\include\xmemory(1777,35): message : see reference to function template instantiation 'void std::_Default_allocator_traits<_Alloc>::construct<_Ty,std::unique_ptr<Base,std::default_delete<Base>>&>(_Alloc &,_Objty *const ,std::unique_ptr<Base,std::default_delete<Base>> &)' being compiled
1> with
1> [
1> _Alloc=std::allocator<std::unique_ptr<Base,std::default_delete<Base>>>,
1> _Ty=std::unique_ptr<Base,std::default_delete<Base>>,
1> _Objty=std::unique_ptr<Base,std::default_delete<Base>>
1> ]
1>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.36.32532\include\xmemory(1831,17): message : see reference to function template instantiation 'void std::_Uninitialized_backout_al<std::allocator<std::unique_ptr<Base,std::default_delete<Base>>>>::_Emplace_back<std::unique_ptr<Base,std::default_delete<Base>>&>(std::unique_ptr<Base,std::default_delete<Base>> &)' being compiled
1>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.36.32532\include\vector(2072,36): message : see reference to function template instantiation 'std::unique_ptr<Base,std::default_delete<Base>> *std::_Uninitialized_copy<std::unique_ptr<Base,std::default_delete<Base>>*,std::unique_ptr<Base,std::default_delete<Base>>*,std::allocator<std::unique_ptr<Base,std::default_delete<Base>>>>(_InIt,_Se,std::unique_ptr<Base,std::default_delete<Base>> *,_Alloc &)' being compiled
1> with
1> [
1> _InIt=std::unique_ptr<Base,std::default_delete<Base>> *,
1> _Se=std::unique_ptr<Base,std::default_delete<Base>> *,
1> _Alloc=std::allocator<std::unique_ptr<Base,std::default_delete<Base>>>
1> ]
1>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.36.32532\include\vector(684,9): message : see reference to function template instantiation 'void std::vector<std::unique_ptr<Base,std::default_delete<Base>>,std::allocator<std::unique_ptr<Base,std::default_delete<Base>>>>::_Construct_n<std::unique_ptr<Base,std::default_delete<Base>>*const &,std::unique_ptr<Base,std::default_delete<Base>>*const &>(const unsigned __int64,std::unique_ptr<Base,std::default_delete<Base>> *const &,std::unique_ptr<Base,std::default_delete<Base>> *const &)' being compiled
1>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.36.32532\include\vector(680,5): message : while compiling class template member function 'std::vector<std::unique_ptr<Base,std::default_delete<Base>>,std::allocator<std::unique_ptr<Base,std::default_delete<Base>>>>::vector(const std::vector<std::unique_ptr<Base,std::default_delete<Base>>,std::allocator<std::unique_ptr<Base,std::default_delete<Base>>>> &)'
1>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.36.32532\include\xmemory(677,47): message : see the first reference to 'std::vector<std::unique_ptr<Base,std::default_delete<Base>>,std::allocator<std::unique_ptr<Base,std::default_delete<Base>>>>::vector' in 'std::_Default_allocator_traits<_Alloc>::construct'
1> with
1> [
1> _Alloc=std::allocator<std::vector<std::unique_ptr<Base,std::default_delete<Base>>,std::allocator<std::unique_ptr<Base,std::default_delete<Base>>>>>
1> ]
1>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.36.32532\include\xmemory(1777,44): message : see the first reference to 'std::_Default_allocator_traits<_Alloc>::construct' in 'std::_Uninitialized_backout_al<std::allocator<std::vector<std::unique_ptr<Base,std::default_delete<Base>>,std::allocator<std::unique_ptr<Base,std::default_delete<Base>>>>>>::_Emplace_back'
1> with
1> [
1> _Alloc=std::allocator<std::vector<std::unique_ptr<Base,std::default_delete<Base>>,std::allocator<std::unique_ptr<Base,std::default_delete<Base>>>>>
1> ]
1>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.36.32532\include\xmemory(1831,31): message : see the first reference to 'std::_Uninitialized_backout_al<std::allocator<std::vector<std::unique_ptr<Base,std::default_delete<Base>>,std::allocator<std::unique_ptr<Base,std::default_delete<Base>>>>>>::_Emplace_back' in 'std::_Uninitialized_copy'
1>C:\Users\xxx\source\repos\ConsoleApplication1\ConsoleApplication1.cpp(30,1): message : see reference to class template instantiation 'std::vector<std::unique_ptr<Base,std::default_delete<Base>>,std::allocator<std::unique_ptr<Base,std::default_delete<Base>>>>' being compiled
1>Done building project "ConsoleApplication1.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
========== Build started at 2:48 PM and took 00.963 seconds ==========
I know that unique_ptr can not copy, but is there a way I can use the vector of unique_ptr as value inside a std::map?
emptyCacheLine.push_back({})
, cause currently it will get runtime error when call cache["tag"].back()
in add_to_cache
. Thanks a lot!For cache type std::map<std::string, std::vector<std::vector<std::unique_ptr<Base>>>>
, you can initialize the innermost vector with one empty vector in the constructor like this:
class Storage {
public:
std::map<std::string, std::vector<std::vector<std::unique_ptr<Base>>>> cache;
void add_to_cache(std::unique_ptr<Base> ptr) {
if (cache["row"].empty())
cache["row"].emplace_back(); // Add an empty vector if none exists
cache["row"].back().push_back(std::move(ptr));
}
void add_new_cacheLine() {
cache.emplace("row", std::vector<std::vector<std::unique_ptr<Base>>>{});
}
Storage() {
cache["row"].push_back(std::vector<std::unique_ptr<Base>>()); // To initialize with one empty vector
}
};
When you call add_to_cache
, it will check if the innermost vector inside the map is empty. If it is, it adds an empty vector before pushing the unique_ptr
into the vector. And in the constructor, we initialize cache["row"]
with one empty vector.