Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

const pointers in STL containers

Hello fellow C++ programmers. I have, what I hope to be, a quick question about STL containers:

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

This statement compiles fine when used in some C++ sourcefile (with the appropriate includes). But

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

or

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

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

A little research unearths that elements in STL containers have to be Assignable. Is there a STL bug in the VC implementation (I'd say: 'unlikely') or do they use another concept of Assignable?

like image 1000
mats Avatar asked Dec 28 '25 22:12

mats


1 Answers

Technically, container elements do have to be assignable, however in std::list, list nodes are very rarely moved around, so once constructed they don't need to be copied (OK) or assigned (would cause an error).

Unless a compiler goes out of its way to test assignability, it's likely that instantiating many list operations won't actually cause a compile error, even if it's not technically legal.

like image 72
CB Bailey Avatar answered Dec 31 '25 12:12

CB Bailey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!