使用 std::unique_ptr 返回 std::unordered_map 的引用时出错

问题描述 投票:0回答:1

我想知道为什么在编译以下示例代码时会出现此错误:

#include <unordered_map>
#include <memory>

class OneClass
{

};

using TestUnorderedMap = std::unordered_map<int, std::unique_ptr<OneClass>>;

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<const int,std::unique_ptr<OneClass,std::default_delete<OneClass>>>::pair(const std::pair<const int,std::unique_ptr<OneClass,std::default_delete<OneClass>>> &)': 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<const int,std::unique_ptr<OneClass,std::default_delete<OneClass>>>::pair'
  D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\include\utility(253): note: 'std::pair<const int,std::unique_ptr<OneClass,std::default_delete<OneClass>>>::pair(const std::pair<const int,std::unique_ptr<OneClass,std::default_delete<OneClass>>> &)': function was implicitly deleted because a data member invokes a deleted or inaccessible function 'std::unique_ptr<OneClass,std::default_delete<OneClass>>::unique_ptr(const std::unique_ptr<OneClass,std::default_delete<OneClass>> &)'
  D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\include\memory(3288): note: 'std::unique_ptr<OneClass,std::default_delete<OneClass>>::unique_ptr(const std::unique_ptr<OneClass,std::default_delete<OneClass>> &)': 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<int,std::unique_ptr<OneClass,std::default_delete<OneClass>>,std::hash<int>,std::equal_to<int>,std::allocator<std::pair<const int,std::unique_ptr<OneClass,std::default_delete<OneClass>>>>>' 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<int,std::unique_ptr<OneClass,std::default_delete<OneClass>>,std::hash<int>,std::equal_to<int>,std::allocator<std::pair<const int,std::unique_ptr<OneClass,std::default_delete<OneClass>>>>>::unordered_map(const std::unordered_map<int,std::unique_ptr<OneClass,std::default_delete<OneClass>>,std::hash<int>,std::equal_to<int>,std::allocator<std::pair<const int,std::unique_ptr<OneClass,std::default_delete<OneClass>>>>> &)'
  D:\Users\Rui Barreiros\Documents\Projectos\Audio\rSensors\rSensorServer_V2\test.cpp(27): note: see the first reference to 'std::unordered_map<int,std::unique_ptr<OneClass,std::default_delete<OneClass>>,std::hash<int>,std::equal_to<int>,std::allocator<std::pair<const int,std::unique_ptr<OneClass,std::default_delete<OneClass>>>>>::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<std::_Umap_traits<_Kty,_Ty,std::_Uhash_compare<_Kty,_Hasher,_Keyeq>,_Alloc,false>>::_Hash<std::allocator<std::_List_node<std::pair<const int,std::unique_ptr<OneClass,std::default_delete<OneClass>>>,std::_Default_allocator_traits<_Alloc>::void_pointer>>>(const std::_Hash<std::_Umap_traits<_Kty,_Ty,std::_Uhash_compare<_Kty,_Hasher,_Keyeq>,_Alloc,false>> &,const _Any_alloc &)' being compiled
          with
          [
              _Kty=int,
              _Ty=std::unique_ptr<OneClass,std::default_delete<OneClass>>,
              _Hasher=std::hash<int>,
              _Keyeq=std::equal_to<int>,
              _Alloc=std::allocator<std::pair<const int,std::unique_ptr<OneClass,std::default_delete<OneClass>>>>,
              _Any_alloc=std::allocator<std::_List_node<std::pair<const int,std::unique_ptr<OneClass,std::default_delete<OneClass>>>,std::_Default_allocator_traits<std::allocator<std::pair<const int,std::unique_ptr<OneClass,std::default_delete<OneClass>>>>>::void_pointer>>
          ]
  ...snip...
c++ reference unique-ptr unordered-map
1个回答
0
投票

弄清楚发生了什么,感谢@Yksisarvinen 和@NathanOliver,我正在使用创建返回引用的副本

TestUnorderedMap map = t.getMap();

并且应该使用

TestUnorderedMap& map = t.getMap();

通过使用前一个,它将创建引用的副本到新的内存位置,第二个是实际引用。

© www.soinside.com 2019 - 2024. All rights reserved.