Constant pointers in STL containers

Welcome C ++ programmers. I have, what I hope, a quick question about STL containers:

std::list<std::string> l;

This statement compiles fine when used in some C ++ source files (with corresponding inclusions). But

std::list<const std::string> m;

or

std::list<std::string * const> n;

cannot compile when using gcc (gcc version 4.0.1 (Apple Inc. build 5484)). However, using the Visual Studio 2008 C ++ compiler, there are no complaints.

A little research shows what elements in STL containers should be Assignable. Is there a STL bug in the VC implementation (I would say "unlikely") or do they use a different concept Assignable?

+3
source share
3 answers

, std::list , () ( ).

, , , .

+4

, (.. const), , , const. , .

+4

STL , semantic (copy c-tor operator =). , const , semantic.

:

std::list<const std::string> m;

Fails because you cannot assign a value to a list item.

+2
source

Source: https://habr.com/ru/post/1714156/


All Articles