[C++] Is it safe?

Started by
17 comments, last by johdex 16 years, 6 months ago
Quote:Original post by Antheus
The fact that compiler is giving you trouble, and that you need to use reinterpret_cast to avoid it is a good hint that it's not a good idea.

Const is optional. It doesn't really do much for the code itself, but it's a strong reminder to users of the code that something either may or may not be modified.

Yes, you can get around the limitation like this, but it's very easy to miss something that will cause problems down the line.

I encountered const-correctness problems in component model. While I designed lower levels rigidly with const members in mind, a subtle flaw emerged 2 layers away. It took me quite a while before I could come up with scenario that would cause problems if I violated constness, and it turned out it wouldn't be that hard to encounter that in real code.

So the fact you encountered problems with this conversion is a good thing. It's pointing out that you should do something. Even if "some stuff" may seem irrelevant here and now, you are deliberately ignoring something important your compiler is telling you.



There are no reinterpret_cast's. Just const_cast, and it's not incorrect to use them. When you derive from class you create a new class with more capabilities. E.g. elements of array<const int> can only be read. Now you derive from it, making array<int> and you give it's elements ability to be modified. Now const is the opposite. An int can be modified and read, but by adding const you add a limitation. That's why you have to make a lot of const_cast's in array<int> - because you have to add more functionality and there is no type modifier that removes constness.
Advertisement
Quote:Original post by rozz666
[...]I understand, but my point is that sometimes you need to manipulate the arrays not just iterators. You need to transfer ownership, etc. Do you suggest that there's no need for any array<const T>?[...]
I don't understand why you ever need 'array<const T>'

Any situation I can think of where that would make sense, 'const array<T>' makes just as much if not more sense. The approach works perfectly for vector, and it really really makes sense to keep the object type the same if you're going to be tracking references. Changing the object's type will make the code very very confusing, and changing template parameters is doing exactly that - changing the object's type.
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
Quote:Original post by Extrarius
Quote:Original post by rozz666
[...]I understand, but my point is that sometimes you need to manipulate the arrays not just iterators. You need to transfer ownership, etc. Do you suggest that there's no need for any array<const T>?[...]
I don't understand why you ever need 'array<const T>'

Any situation I can think of where that would make sense, 'const array<T>' makes just as much if not more sense. The approach works perfectly for vector, and it really really makes sense to keep the object type the same if you're going to be tracking references. Changing the object's type will make the code very very confusing, and changing template parameters is doing exactly that - changing the object's type.


vector<T> is a container, where array<T> is a "pointer". When you write a1 = a2 you don't copy the content, just the reference. array<T> is intended to work almost exactly like T *, so you need array<const T> everywhere you need const T *.
And by deriving, every array<T> is also array<const T>.
In that case, why not use boost::shared_array or boost::shared_ptr<vector> (the latter being closer to your custom class)?
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
Quote:Original post by Extrarius
In that case, why not use boost::shared_array or boost::shared_ptr<vector> (the latter being closer to your custom class)?


Because they don't provide const corectness.
How not? a const shared_array<T> will only give you a const pointer, so you won't be able to modify the data without using a const cast, and you can easily convert from a non-const shared_array<T> to a const shared_array<T> (and it will be done implicitly).

If you insist on doing it yourself, make ref_cnt mutable (it's not part of the visible state, after all) and then just use a const array<T> where the data shouldn't be changed. Resize on an array of constants doesn't make sense (except to shrink it, perhaps, but that would be an odd situation) so a const array should work everywhere you don't want the contents to be changed.
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
Quote:Original post by Extrarius
How not? a const shared_array<T> will only give you a const pointer, so you won't be able to modify the data without using a const cast, and you can easily convert from a non-const shared_array<T> to a const shared_array<T> (and it will be done implicitly).


Is the opposite is also true?
const shared_array<int> a1;shared_array<int> a2;a2 = a1;


Quote:

If you insist on doing it yourself, make ref_cnt mutable (it's not part of the visible state, after all) and then just use a const array<T> where the data shouldn't be changed. Resize on an array of constants doesn't make sense (except to shrink it, perhaps, but that would be an odd situation) so a const array should work everywhere you don't want the contents to be changed.


But it won't work. const array<int> means a constant array of integer, which means a constant pointer. array<const int> means an array of constant integers. It means you can modify/copy the array objects, you just can't modify the integers. The same difference as int *const and const int *.

This would work if the = operator copied the content, but it doesn't. It copies the pointer, so const array<T> is just T *const.
I thought I found a flaw :-)

consider:
void swap(array<const T>& , array<const T>& );array<int> a1;array<const int> a2;swap(a1, a2); // works!


but it's "normal":

void swap(const int *& a1, const int *& a2);int x = 2, *p1 = &xconst int y = 3, *p2 = &yswap(p1, p2); // also works


So in both cases you will be able to modify a const object without const_cast.
It's not safe. Someone may later provide specialized versions of your class templates that use different memory layouts between the const and non-const versions. Your cast() function will then break silently.

-- Top10 Racing Simulation needs more developers!http://www.top10-racing.org

This topic is closed to new replies.

Advertisement