Reputation: 971
std::vector::push_back(constT& value)
requires the type T to be CopyInsertable according to this .
However, compiling the following program with failes (clang, GCC, Visual; both without c++11) unless I provide a public assignment operator.
#include <vector>
class A {
A& operator= (const A& rhs); //private !!
};
int main() {
std::vector<A> v;
A a;
v.push_back(a);
}
Why do I need to provide this assignment operator, I was under the impression that the copy construct was enough.
P.S. I could not find the place in the standard where this is defined, so if you could point to the reference, I would be most grateful
Upvotes: 9
Views: 1425
Reputation: 227418
The reference you quote applies to C++11. However, the C++03 standard has stricter requirements on types that can be stored in containers:
23.1 Container requirements [lib.container.requirements]
...
The type of objects stored in these components must meet the requirements of CopyConstructible types (20.1.3), and the additional requirements of Assignable types.
(emphasis mine.) These requirements have been greatly relaxed in C++11, and are usually expressed in terms of the specific operations performed on containers. In that standard, your code would be valid, since the only requirement would be that A
be CopyInsertable
.
Upvotes: 12