Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cast vector<T> to vector<const T>

Tags:

c++

constants

I have a member variable of type vector<T> (where is T is a custom class, but it could be int as well.) I have a function from which I want to return a pointer to this vector, but I don't want the caller to be able to change the vector or it's items. So I want the return type to be const vector<const T>*

None of the casting methods I tried worked. The compiler keeps complaining that T is not compatible with const T.

Here's some code that demonstrates the gist of what I'm trying to do;

vector<int> a;
const vector<const int>* b = (const vector<const int>* ) (&a);

This code doesn't compile for me.

Thanks in advance!

like image 798
user345386 Avatar asked May 19 '10 18:05

user345386


3 Answers

If you have a const vector<int> you cannot modify the container, nor can you modify any of the elements in the container. You don't need a const vector<const int> to achieve those semantics.

like image 114
James McNellis Avatar answered Oct 05 '22 23:10

James McNellis


In addition to James's answer about how to do it you should note that const int is not a valid type to put into any standard container since it is not assignable.

like image 20
Edward Strange Avatar answered Oct 05 '22 23:10

Edward Strange


On why a vector<T> cannot be correctly converted to a vector<const T> even if T can be converted to const T

This is a common recurring problem in programming whether it is with constness or inheritance (a container of derived object cannot be converted to a container of base objects, even if the contained elements themselves can). The problem is that element by element each one of them can be converted, but the container itself cannot without breaking the type system.

If you were allowed to do vector< const T > &vr = my_vector_of_T, then you would be allowed to add elements through vr, and those elements would be constant by definition. But at the same time those same elements would be aliased in my_vector_of_T as non-const elements and could be modified through that interface, breaking constness in the typesystem.

In the particular case of a vector<int> being converted to a vector<const int>, chances are that you would not notice really weird effects --besides adding an element to a vector<const int> and seeing how the constant element changes in time, but still remember that given two related types T1 and T2 for which a relation exists, in most cases trying to apply the same relationship to containers of T1 and T2 will break the type system.

like image 40
David Rodríguez - dribeas Avatar answered Oct 05 '22 23:10

David Rodríguez - dribeas