problems with const pointers to const objects

T

Tim Partridge

Why would this be illegal?

#include <list>
using namespace std;

int main ( char argc, char* argv[] ) {
list< const int * const > l;
return 0;
}

Doesn't it make sense to be able to have a list of constant pointers that
point to constant objects, or am I missing something?

Clueless,
Tim Partridge
 
V

Victor Bazarov

Tim Partridge said:
Why would this be illegal?

#include <list>
using namespace std;

int main ( char argc, char* argv[] ) {
list< const int * const > l;
return 0;
}

Doesn't it make sense to be able to have a list of constant pointers that
point to constant objects, or am I missing something?

Standard containers require contained things to be Assignable (and
Copy-Constructible). While constant things are copy-constructible,
they are not assignable.

Victor
 
J

Jonathan Turkanis

Victor Bazarov said:
Tim Partridge said:
Why would this be illegal?

#include <list>
using namespace std;

int main ( char argc, char* argv[] ) {
list< const int * const > l;
return 0;
}

Doesn't it make sense to be able to have a list of constant pointers that
point to constant objects, or am I missing something?

Standard containers require contained things to be Assignable (and
Copy-Constructible). While constant things are copy-constructible,
they are not assignable.

Incidentally, one of the standard workarounds for storing
non-copyconstructable or non-assignable types in standard containers
is to store pointers to instances of those types.

In this case, that means using list< const int * const* >. ;-)

Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top