C++模板类重载[]运算符

C++ template class overload [] operator

本文关键字:运算符 重载 C++      更新时间:2023-10-16

我想根据模板参数重载模板类的[]运算符。像这样:

template<
    typename T,
    template<typename> class Property, 
    template<typename> class Key1, 
    template<typename> class Key2>
class a_map 
{
public:
    const Property<T>& operator[](const Key1<T>& k) const
    { return _values[k.index()]; }
    const Property<T>& operator[](const Key2<T>& k) const
    { return _values[k.index()]; }
protected:
    std::vector<Property<T> > _values;
};

我会这样使用这个类:

int main()
{
    a_map<float, prop, key_a, key_b> pm;
}

基本上,我希望能够访问_values向量内部的元素,而不必太担心Key类型。重要的是他们有一个index()成员。

然而,我得到以下错误

错误C2535:"const Property&a_map::运算符[](const Key1&)const':已定义成员函数或申报

即使CCD_ 4和CCD_。

我是不是错过了什么?编译器是否担心在某些情况下Key1<T>Key2<T>实际上可能是同一类型?

编辑这些是main 中使用的类模板

template<typename T>
struct prop 
{
    T weight;
    T height;
};

template<typename T>
class key_a
{
public:
    int index() { return _i; }
private:
    int _i;
};
template<typename T>
class key_b
{
public:
    int index() { return 3; } // Always return 3

编辑我使用的是MVC++2008编译器。

由于除了参数类型之外,您的两个运算符[]都是相同的,为什么不对它们进行模板化呢?

    template <typename TT>
    const Property<T>& operator[](const TT& k) const
    {
        return _values[k.index()];
    }

您需要将index()函数声明为const,因为在a_map模板中,您通过const对象调用它们

template<typename T> class key_a {
public:
    int index() const // <- `const` is necessary
      { return _i; }
private:
    int _i;
};
template<typename T> class key_b {
public:
    int index() const // <- `const` is necessary
      { return 3; } 
};

但除此之外,一切都很顺利。


在VS2010编译器中尝试过它,但得到了与您相同的错误。这显然是MSVC++编译器中的一个编译器错误。模板模板参数的处理未正确实现。

为了解决这个问题,我能够使用这种技术

template<
    typename T,
    template<typename> class Property, 
    template<typename> class Key1, 
    template<typename> class Key2>
class a_map 
{
public:
    const Property<T>& operator[](const typename Key1<T>::self& k) const
    { return _values[k.index()]; }
    const Property<T>& operator[](const typename Key2<T>::self& k) const
    { return _values[k.index()]; }
protected:
    std::vector<Property<T> > _values;
};

并将关键模板定义为

template<typename T> class key_a {
public:
    typedef key_a self;
    int index() const { return _i; }
private:
    int _i;
};
template<typename T> class key_b {
public:
    typedef key_b self;
    int index() const { return 3; } 
};

这是不雅的,但它使它与MSVC++编译器正确工作。

编译如下。。。注意Prop/K1/K2应该如何定义。

#include <vector>
template<
    typename T,
    template<typename> class Property, 
    template<typename> class Key1, 
    template<typename> class Key2>
class a_map 
{
public:
    const Property<T>& operator[](const Key1<T>& k) const
    { return _values[k.index()]; }
    const Property<T>& operator[](const Key2<T>& k) const
    { return _values[k.index()]; }
protected:
    std::vector<Property<T> > _values;
};
template <typename T> struct K1 { int index() const { return 0; } };
template <typename T> struct K2 { int index() const { return 0; } };
template <typename T> struct Prop { };
int main()
{
    a_map<float, Prop, K1, K2> m;
}