Ivan Vecerina said:
The standard does not allow it - but it doesn't either
explicitly require the use of that technique to prevent it.
There is a number of similar safeguards that the standard
committee could have included, but never bothered to.
For example, "mix-in" base classes such as the 'struct iterator'
template would best have a protected destructor, to avoid
dangerous deletion through a base class pointer.
The reality is that these benevolent people have limited
resources, and a lot of higher-priority tasks to deal with.
Agreed. But additionally the committee has little motivation to go
around requiring diagnostics on things that are likely to just fail at
compile time anyway. Especially if it is an area which could possibly
have extended functionality in a future standard. We do not want to
unnecessarily set up backwards compatibility problems for ourselves.
For example see:
http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#276
which will make the OP's code legal in C++0X if the container type
changes to std::list. It is even likely to work today on all
implementations.
Indeed, I hope to see the requirements for a container's value_type
significantly relaxed for all of the containers in C++0X:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1771.html
23.1 - Container requirements
-3- The type of objects stored in these components must meet the requirements
of MoveConstructible and MoveAssignable types. Additionally for some member
functions (as noted below), the types must meet the requirements of
CopyConstructible and/or CopyAssignable types...
These changes would not make vector<const T> legal, but they would allow
a greater range of T to be put in a vector (a const T is not
MoveAssignable).
Another possibility for today is:
std::tr1::array<const int, 3> v = {1, 2, 3};
If your implementation does not provide std::tr1::array, perhaps you
could use boost::array instead:
http://www.boost.org/doc/html/array.html
All that being said, a safe alternative today is still const vector<T>.
-Howard