Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiler error reported for const vector<const T> in VS 2015 but not VS 2013

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?

like image 897
ksl Avatar asked Sep 30 '15 11:09

ksl


1 Answers

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.

like image 58
sp2danny Avatar answered Sep 22 '22 16:09

sp2danny