The following code compiles OK using Visual Studio 2013.
#include <vector>
#include <string>
int main()
{
const std::string constString("fred");
const std::vector<const std::string> myVector{ constString };
}
If I try to compile it using Visual Studio 2015 the following error is reported:
1>xmemory0(587): error C2338: The C++ Standard forbids containers of const elements because allocator<const T> is ill-formed.
I've seen various posts, and in particular this one Does C++11 allow vector<const T>?, about vector<const T>
and why it's not allowed but I don't really get it. However, in the above example the vector itself is const.
Can someone please explain? Is VS 2013 wrong to compile it successfully?
Standard Requirement for T
======== =================
C++03 any type
C++11 any non-const, non-reference object type
C++14 any non-const object type
C++17 any cv-unqualified object type
Nowhere in the standard does it make an exception for
when the container itself is const
So yes, VS 2013 is wrong to compile it successfully.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With