-2

I tried to initialize a list of const string but I get an error. The simply code is the following

#include <string>
#include <list>

int main()
{
    std::list<const std::string> l;
    return 0;
}

When I compile I get the following error:

enter image description here

Can someone explain me why? Thanks

ochs.tobi
  • 3,214
  • 7
  • 31
  • 52
Simone
  • 11
  • The allocator requirements need `T` needs to be a "non-const, non-reference object type – P0W May 07 '19 at 11:16
  • Possible duplicate of [Initialize a static const list of strings](https://stackoverflow.com/questions/34154732/initialize-a-static-const-list-of-strings) – Masoud Keshavarz May 07 '19 at 11:21

1 Answers1

1

std::list must have a non-const and non-volatile value type by definition. You can initialize the list itself as const, so the content and length isn't changeable e.g.:

const std::list<std::string> l = {"a","b"};
Korni
  • 464
  • 2
  • 10