| re: Cast vector<foo*> to vector<const foo*>?
"Joseph Turian" <turian@gmail.com> wrote in message
news:1138259627.785419.178590@g44g2000cwa.googlegr oups.com[color=blue]
> Ian Collins wrote:[color=green][color=darkred]
>>> How can I cast a vector<foo*> to vector<const foo*>?[/color]
>> You can't; they are completely different types.[/color]
>
> Care to edify me why?
> At first blush, they seem quite similar.
>
> Joseph[/color]
To add to what Ian has said,
foo * and const foo * are different types but are nevertheless similar
enough that you can use const_cast to cast between them. Where two types are
used as template arguments, however, the similarity of the types is
irrevant. Consider:
template<class T>
class Test
{
T t;
};
template<>
class Test<const int>
{
char array[1000];
};
Thus Test<const int> contains an array of chars, whereas Test<int> contains
a single int.
#include <iostream>
using namespace std;
int main()
{
Test<int> t1;
Test<const int> t2;
cout << sizeof(t1) << endl; // gives 4
cout << sizeof(t2) << endl; // gives 1000
return 0;
}
In reality vector<foo *> and vector<const foo *> may hardly differ at
all --- in particular, they may have the same size. However, the possibility
of explicit template specialization means that they could differ
spectacularly, hence the reluctance of the compiler to allow conversion.
--
John Carson |