C++模板类重载[]运算符
C++ template class overload [] operator
我想根据模板参数重载模板类的[]运算符。像这样:
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;
}
相关文章:
- 使用C++中的模板和运算符重载执行矩阵运算
- 为什么这个运算符<重载函数对 STL 算法不可见?
- <T> 通过模板化运算符重载将 std::complex 乘以双倍
- C++20概念:需要运算符重载
- 使用赋值运算符重载从类中返回jobject
- 在运算符重载定义中使用成员函数(const错误)
- 字节到位运算符重载C++
- 为什么在运算符重载时需要参考?
- 类中 c++ 的运算符 + 重载
- 算术复合运算符重载为非成员
- 运算符重载 (+),用于添加两个具有 C++ 的数组
- 交换运算符 + 重载会导致无限递归
- 如何理解新的运算符重载?
- 向量保持复数的运算符重载
- 如何创建运算符重载?
- 链接列表运算符重载没有打印出我想要的内容
- C++:需要帮助了解运算符重载错误
- 使用模板化运算符重载 XOR 运算符失败
- 如何确保接受的C++模板类型使运算符重载?
- 运算符重载使用运算符+添加类模板