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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top