vector<const T> vs. const vector<T> vs. const vector<const T>

What's the difference between:

vector<const string>
const vector<string>
const vector<const string>

Let's say I have a vector of strings of the name of each month declared and defined in the global scope. The vector is to be used by a lot of parts of the application. I want it the values to be constant, what kind of const should I use?
vector<const string>: Cannot change the values of the elements of this vector after they've been added. EDIT: This may fail to compile on a pre-C++11 compiler.

const vector<string>: Cannot change anything about the vector.

const vector<const string>: See #2.

Last edited on
I tried compiling an example with const vector<const string> and the compilation failed. I can't explain why, so I'm hoping someone else will.

You should use a const vector<string> and the vector being const should prevent you from changing its contents.

Edit: dun goof'd.
Last edited on
The type of the element of a sequence container must be at least MoveAssignable, several operation require that it also must be CopyAssignable

Therefore, std::vector<const std::string> is badly formed.

The logical state of a sequence container includes the state of the elements which makes up the sequence; so in a const std::vector<std::string>, the container itself (vector) as well as the elements it contains (strings) are immutable.
closed account (o1vk4iN6)
const std::vector<const std::string> works with vs2013. GCC seems to give some weird internal error though.
Thanks to everyone who replied! Now I understand everything.
Topic archived. No new replies allowed.