std::vector<const int> vci;
vci.push_back(1);
vci[0] = 2;
With the element type being const int, shouldn’t the assignment statement be assigning to a const int&? This does not compile with LLVM 3.0. Why does VC++ allow it?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
While it is Undefined Behavior and basically anything can happen including what you are seeing, I have tracked this to what seems to be an incompatibility of the library with the standard. In particular the standard allocator defined in the VS2010 library does not conform with the standard.
The standard dictates that
std::vector<T,Allocator>::value_typeis a typedef toAllocator::value_type. Now the default allocator (if none is provided) isstd::allocator<T>for whichvalue_type, according to Table 28 must be Identical to T. Now the implementation of the standard allocator in VS2010 drops theconstqualifier from the type argument, sostd::allocator<const T>::value_typeisT, and notconst T.It is important to note that the compiler is not non-conformant for accepting the code that you provided per-se, since it is Undefined Behavior and the compiler is free to do as it pleases. But on the other hand, there is a non-conformity in the
std::allocatorimplementation.You have already answered the question yourself: It is Undefined Behavior. The compiler does not need to provide a diagnostic, and the result of the operation can be anything. It is a case of quality of implementation to detect that the type is not assignable and provide a meaningful error message (or not)