Ошибка при возврате ссылки на std::unordered_map с помощью std::unique_ptr.C++

Программы на C++. Форум разработчиков
Anonymous
Ошибка при возврате ссылки на std::unordered_map с помощью std::unique_ptr.

Сообщение Anonymous »

Мне интересно, почему возникает эта ошибка при компиляции следующего примера кода:
#include
#include

class OneClass
{

};

using TestUnorderedMap = std::unordered_map;

class TestClass
{
public:
TestUnorderedMap& getMap() { return m_map; }
private:
TestUnorderedMap m_map;
};

void simpleTestFunction()
{
TestClass t;
TestUnorderedMap map = t.getMap();
}


Я получаю эту ошибку при компиляции с помощью Visual Studio 2022. Могу ли я вернуть ссылку на unordered_map, которая использует std::unique_ptr в качестве объект?
D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\include\xutility(241): error C2280: 'std::pair::pair(const std::pair &)': attempting to reference a deleted function
D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\include\utility(253): note: see declaration of 'std::pair::pair'
D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\include\utility(253): note: 'std::pair::pair(const std::pair &)': function was implicitly deleted because a data member invokes a deleted or inaccessible function 'std::unique_ptr::unique_ptr(const std::unique_ptr &)'
D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\include\memory(3288): note: 'std::unique_ptr::unique_ptr(const std::unique_ptr &)': function was explicitly deleted
D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\include\xutility(241): note: the template instantiation context (the oldest one first) is
D:\Users\Rui Barreiros\Documents\Projectos\Audio\rSensors\rSensorServer_V2\test.cpp(20): note: see reference to class template instantiation 'std::unordered_map' being compiled
D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\include\unordered_map(110): note: while compiling class template member function 'std::unordered_map::unordered_map(const std::unordered_map &)'
D:\Users\Rui Barreiros\Documents\Projectos\Audio\rSensors\rSensorServer_V2\test.cpp(27): note: see the first reference to 'std::unordered_map::unordered_map' in 'simpleTestFunction'
D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\include\unordered_map(111): note: see reference to function template instantiation 'std::_Hash::_Hash(const std::_Hash &,const _Any_alloc &)' being compiled
with
[
_Kty=int,
_Ty=std::unique_ptr,
_Hasher=std::hash,
_Keyeq=std::equal_to,
_Alloc=std::allocator,
_Any_alloc=std::allocator
]
...snip...


Подробнее здесь: https://stackoverflow.com/questions/784 ... unique-ptr

Вернуться в «C++»