The following code generates the compiler error below (after the code), but not if the vector contains unique_ptr’s directly (see commented code lines). Any ideas why?
The question is more concerned with the code block in the “#if 1” block, the “#else” block generates the error (after changing “#if 1” to “#if 0”) that is similar, but is more expected.
// MoveSemantics.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <memory>
#include <vector>
typedef std::unique_ptr<int> upi;
struct S
{
S() : p(new int(123)) {}
S(S&& s) : p( std::move(s.p) ) {} // NB: the move constructor is supposed to be used? (but not)
upi p;
};
#if 1
void test()
{
//std::vector<S> vs; // Okay
//std::vector<upi> vupi(10); // Okay
std::vector<S> vs(10); // Error! why in the hell does the compiler want to generate a copy constructor here??
}
#else
void test()
{
std::vector<S> vs;
vs.push_back( S() );
const S& s = vs.front();
//S& s = vs.front(); // fine!
S s1 = std::move(s); // Error, but expected
}
#endif
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
Compiler Error:
1> error C2248: 'std::unique_ptr<_Ty>::operator =' : cannot access private member declared in class 'std::unique_ptr<_Ty>'
1> with
1> [
1> _Ty=int
1> ]
1> c:\program files\microsoft visual studio 11.0\vc\include\memory(1435) : see declaration of 'std::unique_ptr<_Ty>::operator ='
1> with
1> [
1> _Ty=int
1> ]
1> This diagnostic occurred in the compiler generated function 'S &S::operator =(const S &)'
This looks like a bug in your std::lib. I’m sure it got here because of the history of the evolving
vectorspecification.In C++98/03
vectorhad this constructor:And the specification was that the
Twould be default constructed once and then copy constructedntimes when called with the latter two arguments defaulted.In C++11 this got changed to:
The spec for the 2nd constructor didn’t change. But the first did: It should default construct
T ntimes, and not copy (or move) it at all.I would have expected the error message to say that the deleted or private copy constructor of
unique_ptrwas being used. That would have indicated that thevectorwas following the C++98/03 spec, and simply hasn’t been updated yet.But since the diagnostic is complaining about
unique_ptr‘s copy assignment instead, then it looks like thevectorhas been updated, but incorrectly. It sounds like it is using this signature from C++98/03:and default constructing
nT‘s, and then assigningvalueto thosenT‘s.