奇怪的错误 - 为什么编译器试图调用复制构造函数?

ron*_*nag 7 c++ compiler-errors visual-studio-2010 c++11

我收到了一些非常奇怪的错误.编译器似乎想要因为某些我不理解的原因而调用复制构造函数.

(118) std::map<int, layer> xs;
(119) xs.begin()->first; // error?!
Run Code Online (Sandbox Code Playgroud)

layer 是一种不可复制的可移动类型.

class layer : public observable
{
    layer(const layer&);
    layer& operator=(const layer&);
public:
    layer(int index = -1);
    layer(layer&& other);
    layer& operator=(layer&& other);
   //...
};
Run Code Online (Sandbox Code Playgroud)

由于某种原因,第119行导致编译器尝试调用复制构造函数std::pair,为什么?

1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(131): error C2248: 'layer::layer' : cannot access private member declared in class 'layer'
1> ..\layer.h(55) : see declaration of 'layer::layer'
1> ..\layer.h(53) : see declaration of 'layer'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(129) : while compiling class template member function 'std::_Pair_base<_Ty1,_Ty2>::_Pair_base(const std::_Pair_base<_Ty1,_Ty2> &)'
1> with
1> [
1>     _Ty1=const int,
1>     _Ty2=layer
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(174) : see reference to class template instantiation 'std::_Pair_base<_Ty1,_Ty2>' being compiled
1> with
1> [
1>     _Ty1=const int,
1>     _Ty2=layer
1> ]
1> ..\stage.cpp(119) : see reference to class template instantiation 'std::pair<_Ty1,_Ty2>' being compiled
1> with
1> [
1>     _Ty1=const int,
1>     _Ty2=layer
1> ]
Run Code Online (Sandbox Code Playgroud)

我也试过以下,它也失败了.

(118) std::map<int, layer> xs;
(119) auto& t1 = *xs.begin();
(120) auto& t2 = t1.first; // error?!
Run Code Online (Sandbox Code Playgroud)

这里发生了什么?

Tho*_*din 3

这是模板错误的奇怪微妙之处之一。模板代码不是代码,它几乎更接近于生成代码的脚本语言。您甚至可能在函数中存在语法错误,但在您的代码(直接或间接)使用该函数之前,该函数不一定会生成编译器错误。

在这种情况下,xs.first() 导致生成 std​​::map<int, layer>::iterator,这也需要生成 std​​::pair<int, layer>。std::pair 的默认实现有一个复制构造函数,该构造函数无法编译。

您可以使用没有复制构造函数的 std::pair 模板专业化来解决此问题,但是您无法将任何内容插入到映射中。xs[0] = myLayer 创建 std::make_pair<0, myLayer> 并将其插入地图中,这显然需要图层的复制构造。

典型的解决方案是将类型更改为 std::map<int, std::shared_ptr<layer>>。复制shared_ptr不会复制引用的对象。