What is the rationale behind C++11 forbidding containers of const elements? I am referring to the following error message, which you get if you define, for example, a vector of const elements:
error C2338: The C++ Standard forbids containers of const elements because allocator<const T> is ill-formed.
If you read similar questions, the answer usually repeats the error message, but never goes into details or even gives an explanation as to why the static_assert
was introduced. Defining containers of const elements doesn't seem to be an unreasonable thing to do, so why would C++11 forbid it? Since it used to be allowed in earlier versions, what is the exact reason for this limitation?
Here is sample code which fails to compile in VS2017:
#include "stdafx.h"
#include <vector>
struct foo {};
typedef std::vector<const foo> foo_vector;
int main(int argc, char** argv)
{
foo_vector v;
return 0;
}
Regarding the possibly duplicate question, it neither provides background nor a reliable answer.