重载类模板的非成员算术
Overloading non-member arithmetic for class templates
我正在尝试重载运算符*以处理使用不同类型的实例化的类模板,但从编译器获得"模板参数列表过多"。这是我的函数实现:
template <typename T>
template <typename E>
inline Vec2<T> operator*(Vec2<T> lhs, Vec2<E>& rhs)
{
lhs *= rhs;
return lhs;
}
template <typename T>
template <typename E>
inline Vec2<T> operator*(Vec2<T> lhs, E scalar)
{
lhs.x *= scalar;
lhs.y *= scalar;
return lhs;
}
这是我使用它的用例:
Vec2<float> scale(0.5, 0.8);
Vec2<short> value(50, 100);
Vec2<short> result = value * scale;
// value should now equal (25, 80)
好吧,您使用的语法不正确。
template <typename T>
template <typename E>
//...
仅在定义模板类的模板成员时使用,但情况并非如此。在您的情况下,您应该简单地使用
template <typename T, typename E> Vec2<T> operator*(Vec2<T> lhs, Vec2<E>& rhs)
相关文章:
- 将重载的成员函数传递给函数模板
- 在运算符重载定义中使用成员函数(const错误)
- 重载运算符的范围是什么?它是否会影响作为类成员的集合的插入函数?
- 解决模板成员函数重载
- 为什么不允许成员函数和非成员函数之间的函数重载?
- 为什么 std::sort 找不到合适的(静态成员)函数重载?
- 如何为非常量和常量重载实现一次成员函数?
- 为什么重载运算符<<打印特征类成员会导致段错误?
- 运算符重载作为成员函数
- 指向重载静态成员的函数指针 - 在unique_ptr中用作自定义删除器
- 将基类的成员函数重载到其他派生类C++
- 重载具有 2 个相同数据类型成员的构造函数
- 关于在成员重载中使用 std::move() 的问题
- 成员函数不能为集合迭代器和const_iterator的输入重载(但可以为其他 STL 迭代器重载)
- 如何正确返回带有成员变量的重载运算符++?
- 在设计方面:重载vector类型的类成员的插入运算符
- 派生类不能用另一个基类的成员重载基类中的私有成员
- 用于测量重载运算符和成员函数的时间的函数
- C++基于模板成员的存在进行成员重载
- 用于类成员重载方法的 SFINAE