HighCommander4 HighCommander4 - 2 months ago 16
C++ Question

Does C++11 allow vector<const T>?

Container requirements have changed from C++03 to C++11. While C++03 had blanket requirements (e.g. copy constructibility and assignability for vector), C++11 defines fine-grained requirements on each container operation (section 23.2).

As a result, you can e.g. store a type that is copy-constructible but not assignable - such as a structure with a const member - in a vector as long as you only perform certain operations that do not require assignment (construction and

push_back
are such operations;
insert
is not).

What I'm wondering is: does this mean the standard now allows
vector<const T>
? I don't see any reason it shouldn't -
const T
, just like a structure with a const member, is a type that is copy constructible but not assignable - but I may have missed something.

(Part of what makes me think I may have missed something, is that gcc trunk crashes and burns if you try to instantiate
vector<const T>
, but it's fine with
vector<T>
where T has a const member).

Answer

No, I believe the allocator requirements say that T can be a "non-const, non-reference object type".

You wouldn't be able to do much with a vector of constant objects. And a const vector<T> would be almost the same anyway.