Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weird error - why is the compiler trying to call the copy constructor?

I'm getting some very wierd errors. The compiler seems to want to call the copy constructor for some reason I don't understand.

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

layer is a non-copyable, movable type.

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

For some reason the line 119 caused the compiler to try to invoke the copy constructor for std::pair, why?

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> ]

I've also tried the following, where it fails similarly.

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

What is going on here?

like image 619
ronag Avatar asked Dec 31 '11 16:12

ronag


1 Answers

This is one of the strange subtleties of template errors. Template code is not code, it's almost closer to a scripting language for generating code. You can even have a syntax error in a function that won't necessarily generate a compiler error until that function is used (directly or indirectly) by your code.

In this case xs.first() caused the generation of std::map<int, layer>::iterator, which also necessitates the generation of std::pair<int, layer>. The default implementation of std::pair has a copy constructor, which fails to compile.

You could get around this with a template specialization of std::pair which does not have the copy constructor, but then you can't insert anything into your map. xs[0] = myLayer creates and inserts std::make_pair<0, myLayer> into your map, which obviously requires the copy construction of a layer.

The typical solutions for this is to change your type to std::map<int, std::shared_ptr<layer> >. Copying a shared_ptr doesn't copy the referenced object.

like image 115
Thomas Bouldin Avatar answered Oct 11 '22 05:10

Thomas Bouldin