c++ - Subscript operator overload for partial template specialisation -
i'm trying learn templates , template specialisation. i'm writing template class arrays, using template specialisation avoid code bloat. have specialised template myarray , inherit 1 class myarray<t*> : private myarray<void*>
. i'm having trouble overloading subscript operators (one non-const refs, 1 const refs). here's piece of code (far complete, contains problem).
#include <iostream> using std::cout; using std::cin; using std::endl; /*** template class myarray **/ template <class t> class myarray {}; /*** full template specialization myarray holding pointers ***/ template <> class myarray<void*> { public: explicit myarray(unsigned s = 100) : sz(s) { data = new void*[s]; } virtual ~myarray() { delete[] data; } /** subscript operator overload non-const refs **/ void*& operator[](unsigned i) { return data[i]; } /** subscript operator overload const refs **/ const void*& operator[](unsigned i) const { return data[i]; // line 26 } unsigned size() const { return sz; } private: void** data; unsigned sz; }; /** partial specialization: create template class inheriting 1 above **/ template <class t> class myarray<t*> : private myarray<void*> { public: explicit myarray(unsigned s = 100) : myarray<void*>::myarray(s) { data = new t*[s]; } virtual ~myarray() { delete[] data; } /** subscript operator overload non-const refs **/ t*& operator[](unsigned i) { return reinterpret_cast<t*&>( // line 47 myarray<void*>::operator[](i) ); } /** subscript operator overload const refs **/ const t*& operator[](unsigned i) const { return reinterpret_cast<const t*&>( myarray<void*>::operator[](i) ); } unsigned size() const { return myarray<void*>::size(); } private: t** data; }; /** input function filling myarray's **/ template <class t> void inputmyarray(myarray<t*>& my_array) { unsigned size = 0; t tmp; while (cin >> tmp) { t* = new t; *i = tmp; my_array[size++] = i; } } /** output function printing elements of myarray's **/ template <class t> void outputarray(const myarray<t*>& my_array) { (unsigned = 0; < my_array.size(); i++) { cout << *my_array[i] << " "; } cout << endl; } int main() { /** initialize array, fill it, print contents **/ myarray<int*> p; inputmyarray(p); cout << "myarray of pointer ints holds int values: " << endl; outputarray(p); return 0; }
the compiler (clang) complaining (error) line 26
non-const lvalue reference type 'const void *' cannot bind value of unrelated type 'void *'
i suppose don't want compiler interpret non-const reference - want const. how can overload operator in context? corresponding piece of code works fine template class without specialisation, myarray<t>
.
the compiler further complains (warnings) reinterpret_cast
s apparently contain undefined behaviour
reinterpret_cast 'void *' 'int *&' has undefined behavior
(line 47). reinterpret_casts
copy-pasted instructions, think used this. don't know why reference void*
isn't picked up. doing wrong here?
your template container void*
, not const void*
:
/** subscript operator overload const refs **/ void* const& operator[](unsigned i) const { return data[i]; // line 26 }
same t*
:
/** subscript operator overload const refs **/ t* const& operator[](unsigned i) const { return reinterpret_cast<t*const&>( myarray<void*>::operator[](i) ); }
Comments
Post a Comment