mercredi 30 septembre 2015

Complier error reported for const vector

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?

Aucun commentaire:

Enregistrer un commentaire