我正在尝试了解模板和模板特化。我正在为数组编写一个模板类,使用模板专门化来避免代码膨胀。因此,我有一个完全专门化的模板 MyArray,然后我从这个模板继承,如 class MyArray<T*> : private MyArray<void*>
.我在重载下标运算符时遇到问题(一个用于非 const 引用,一个用于 const 引用)。这是一段代码(远未完成,但包含我的问题)。
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
/*** template class MyArray **/
template <class T>
class MyArray {};
/*** Full template specialization for 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 for non-const refs **/
void*& operator[](unsigned i) {
return data[i];
}
/** subscript operator overload for 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 the template class by inheriting from the one 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 for non-const refs **/
T*& operator[](unsigned i) {
return reinterpret_cast<T*&>( // line 47
MyArray<void*>::operator[](i)
);
}
/** subscript operator overload for 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 for filling MyArray's **/
template <class T>
void InputMyArray(MyArray<T*>& my_array) {
unsigned size = 0;
T tmp;
while (cin >> tmp) {
T* i = new T;
*i = tmp;
my_array[size++] = i;
}
}
/** output function for printing elements of MyArray's **/
template <class T>
void OutputArray(const MyArray<T*>& my_array) {
for (unsigned i = 0; i < 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 to ints holds int values: " << endl;
OutputArray(p);
return 0;
}
编译器 (clang) 提示(错误)第 26 行
non-const lvalue reference to type 'const void *' cannot bind to a value of unrelated type 'void *'
我想我不希望编译器将其解释为非常量引用——我想要的是它是常量。在这种情况下如何正确重载此运算符?相应的代码片段适用于没有专门化的模板类,如 MyArray<T>
.
编译器进一步提示(警告)关于 reinterpret_cast
显然包含未定义行为的 s
reinterpret_cast from 'void *' to 'int *&' has undefined behavior
(第 47 行)。 reinterpret_casts
本质上是从我的说明中复制粘贴的,所以我认为它们应该像这样使用。我不知道为什么要提到 void*
没有被拾起。我在这里做错了什么?
最佳答案
您的模板是 void*
的容器,而不是 const void*
:
/** subscript operator overload for const refs **/
void* const& operator[](unsigned i) const {
return data[i]; // line 26
}
同样适用于 T*
:
/** subscript operator overload for const refs **/
T* const& operator[](unsigned i) const {
return reinterpret_cast<T*const&>(
MyArray<void*>::operator[](i)
);
}
关于c++ - 部分模板特化的下标运算符重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32862062/