Let\'s say I have
#include
#include
using namespace std;
struct Student
{
const string name;
int grade;
Student(co
You can't. Your type violates the "Assignable" requirement for standard containers.
ISO/IEC 14882:2003 23.1 [lib.container.requirements] / 3:
The type of objects stored in these components must meet the requirements of
CopyConstructibletypes (20.1.3), and the additional requirements ofAssignabletypes.
From table 64 (Assignable requirements):
In Table 64,
Tis the type used to instantiate the container,tis a value ofT, anduis a value of (possiblyconst)T.expression:
t = u; return type:T; post-condition:tis equivalent tou
In theory, a std::vector equivalent could choose to do destruction and copy construction in all cases, but that's not the contract that has been chosen. If reallocation isn't required, then using the contained type's assignment operator for things like vector::operator= and vector::assign might be significantly more efficient.