[C++] Is it safe?

Started by
17 comments, last by johdex 16 years, 6 months ago
Suppose you have:

template <typename T>
class array {
    // some stuff
    // no virtual methods
private:
    T *data;
    size_t size;
    uint *ref_cnt;
};

template <typename T>
class array<const T> {
    // similar stuff
    // no virtual methods
private:
    const T *data;
    size_t size;
    uint *ref_cnt;
};
Now, is it safe to cast pointers to array<T> to array<const T>? E.g.:

template <typename T>;
array<const T> array<const T>::cast(array<T>& rhs)
{
    return *reinterpret_cast<array<const T> *>(&rhs);
}
Advertisement
Depends on the "some stuff", which may alter the sequentiality of your private members.

As a whole, though, people usually prefer to use iterators instead to handle this kind of problem.
"Safe", yes. A good idea? No. Why not give array() a method to return a const version of itslef?
Quote:Original post by Evil Steve
"Safe", yes. A good idea? No. Why not give array() a method to return a const version of itslef?


array<const T> has constructor array(const array<T>& );

However, technically every array<const T> is also an array<T>, so there should be no need for temprary object creation. E.g.

array<int> a;void f1(const array<const int>& );void f2(array<const int>& );f1(a); // temporary object array<const int>f2(a); // warning, and wrong anyways


Oh, now I see your point :-)
Making cast(array<int>& ) and cast(const array<int>& ) won't work.

But I'm thinking of a method to make it implicit.
Maybe:

template <typename T>class array : public array<const T> {  // stuff with many const_cast's :-)};template <typename T>class array<const T> {protected:    const T *data;    size_t size;    uint *ref_cnt;};
Quote:Original post by rozz666
However, technically every array<const T> is also an array<T>


I cannot use an array<const T> as if it were an array<T>, because that would let me change its elements. Arguably, one could consider that an array<T> is an array<const T>, though, so I guess this is what you meant.

Again, however, iterators are a far more elegant way of handling this, by separating the physical storage (the container) from the const/non-const way of accessing the elements (the iterator).
Quote:Original post by ToohrVyk
Quote:Original post by rozz666
However, technically every array<const T> is also an array<T>


I cannot use an array<const T> as if it were an array<T>, because that would let me change its elements. Arguably, one could consider that an array<T> is an array<const T>, though, so I guess this is what you meant.

Yes, that's what I meant.
Quote:

Again, however, iterators are a far more elegant way of handling this, by separating the physical storage (the container) from the const/non-const way of accessing the elements (the iterator).


Yes but generally you don't expect iterators to own objects.
Anyways, my array class isn't supposed to work as a containter but rather like a RAII replacement for T *. The same goes or my ptr class.
Quote:Original post by rozz666
Yes but generally you don't expect iterators to own objects.
Anyways, my array class isn't supposed to work as a containter but rather like a RAII replacement for T *. The same goes or my ptr class.


My point is that you use the array to own the objects, and then manipulate it through iterators (so that you never need to convert an array of non-const to an array of const).
Quote:Original post by ToohrVyk
Quote:Original post by rozz666
Yes but generally you don't expect iterators to own objects.
Anyways, my array class isn't supposed to work as a containter but rather like a RAII replacement for T *. The same goes or my ptr class.


My point is that you use the array to own the objects, and then manipulate it through iterators (so that you never need to convert an array of non-const to an array of const).


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>?
Imagine a string class and imagine you have "some text". Now, you want to use "some text" as a string object. string internally has array<char>. You'll have to create a copy of "some text" and store it the array. However, when you have array<const char> inside the string you can "cast" "some text" to array<const char> making an object with ref_cnt = NULL. And therefore you can "cast" "some tect" into a string object without making a copy.
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.
It seems the solution with deriving classes works!

Here's my code:

template <typename T>class array;template<typename T>class array<const T> {public:    array();    array(size_t size, const T *v);    array(const array<const T>& rhs);protected:    array(size_t size, const T *v, uint *num_refs);public:    static array<const T> cast(const T *v, size_t size);    ~array();    array<const T>& operator=(const array<const T>& rhs);    const T& operator[](int idx) const;    size_t get_byte_size() const;    array<T> copy() const;    const T *raw() const;public:    const size_t size;protected:    const T *data;    uint *num_refs;};template<typename T>class array : public array<const T> {public:    array();    explicit array(size_t size);    array(size_t size, const T *v);    array(const array<T>& rhs);private:    array(size_t size, T *v, uint *num_refs);public:    static array<T> cast(T *v, size_t size);    T& operator[](int idx) const;    T *raw() const;};template <typename T>inline array<T>::array() : array<const T>() { };template <typename T>inline array<T>::array(size_t size) : array<const T>(size, new T[size], new uint(1)) { };template <typename T>inline array<T>::array(size_t size, const T *v) : array<const T>(size, v) { };template <typename T>inline array<T>::array(const array<T>& rhs) : array<const T>(rhs) { };template <typename T>inline array<T>::array(size_t size, T *v, uint *num_refs) : array<const T>(size, v, num_refs) { };template <typename T>inline static array<T> array<T>::cast(T *v, size_t size){    return array<T>(size, v, NULL);};template <typename T>inline T& array<T>::operator[](int idx) const{    conditional::throw_if_array_out_of_bounds<options::throw_array_out_of_bounds>::perform(*this, idx);    return const_cast<T& >(data[idx]);};template <typename T>inline T *array<T>::raw() const{    return const_cast<T *>(data);};template <typename T>inline array<const T>::array() : data(NULL), size(0), num_refs(NULL) { };template <typename T>inline array<const T>::array(size_t size, const T *v)    : size(size), data(new T[size]), num_refs(new uint(1)){    copy(data, v, size);};template <typename T>inline array<const T>::array(const array<const T>& rhs)    : data(rhs.data), size(rhs.size), num_refs(rhs.num_refs){    if (num_refs) ++*num_refs;};template <typename T>inline array<const T>::array(size_t size, const T *v, uint *num_refs)    : data(v), size(size), num_refs(num_refs) { };template <typename T>inline array<const T> array<const T>::cast(const T *v, size_t size){    return array<const T>(size, v, NULL);};template <typename T>inline array<const T>::~array(){    if (num_refs && !--*num_refs) {               delete[] data;        delete num_refs;      }};template <typename T>inline array<const T>& array<const T>::operator=(const array<const T>& rhs){    if (rhs.num_refs) ++*rhs.num_refs;    if (num_refs && !--*num_refs) {        delete[] data;        delete num_refs;    }    data = rhs.data;    const_cast<uint& >(size) = rhs.size;    num_refs = rhs.num_refs;    return *this;};template <typename T>inline const T& array<const T>::operator[](int idx) const{    conditional::throw_if_array_out_of_bounds<options::throw_array_out_of_bounds>::perform(*this, idx);    return data[idx];};template <typename T>inline uint array<const T>::get_byte_size() const{    return size * sizeof(T);};template <typename T>inline array<T> array<const T>::copy() const{    array<T> b(size);    copy(b.data, data, size);    return b;};template <typename T>inline const T *array<const T>::raw() const{    return data;};


array<int> a1;array<const int> a2;a1 = a2; // wrong, doesn't compilea2 = a1; // okvoid f1(array<int>& );void f2(array<const int>& );f1(a1); // okf1(a2); // wrong, doesn't compilef2(a1); // okf2(a2); // ok


No temporary objects are created.

What do you think about it?

[Edited by - rozz666 on October 13, 2007 2:09:13 PM]

This topic is closed to new replies.

Advertisement