const vector<const MyType> Vs const vector<MyType>

M

magnus.moraberg

Hi there,

what is the difference between const vector<const MyType> and const
vector<MyType>?

Thanks,

Barry
 
K

Kai-Uwe Bux

Hi there,

what is the difference between const vector<const MyType> and const
vector<MyType>?

A vector< MyType const > (const or not) invokes undefined behavior since
MyType const does not satisfy the Assignable requirement for containers. On
the other hand, vector<MyType> has this problem only if MyType does not
satisfy the requirements. If you have a good STL implementation, something
like vector< MyType const > might not even compile since concept checks
would fail.

As for const vector<X>, well, it's const: you can only call const methods on
it, which implies that you cannot change the contents of the vector after
construction (which leaves the constructor taking a range of two iterators
as the only interesting way of creating such a critter).


Best

Kai-Uwe Bux
 
A

Andrew Koenig

what is the difference between const vector<const MyType> and const
vector<MyType>?

For one thing, const vector<const MyType> is not legal because the element
type is not assignable.
 

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,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top