繁体   English   中英

下标运算符重载用于部分模板专业化

[英]Subscript operator overload for partial template specialisation

我正在尝试了解模板和模板专业化。 我正在为数组编写模板类,使用模板专业化来避免代码膨胀。 因此,我有一个完全专用的模板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行

对类型'const void *'的非const左值引用不能绑定到不相关的类型'void *'的值

我想我不希望编译器将此解释为非const引用-我想要的是使其成为const。 在这种情况下,如何正确重载该运算符? 对应的代码段对于没有专门化的模板类(如MyArray<T>

编译器进一步抱怨(警告)关于reinterpret_cast ,其中显然包含未定义的行为

从'void *'到'int *&'的reinterpret_cast具有未定义的行为

(第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)
        );
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM