C++模板元编程:重载运算符
C++ template meta programming: overloading operators
我目前正在玩模板元编程。我正在尝试使用 tmp 制作有限状态机。我知道网络上有几个实现,但我想自己实现一个作为练习。
我正在尝试重载基类内模板化基类的模板化派生的运算符。 假设我们有一个基类:
template<typename Input>
class Base
{
public:
virtual ~Base() = default;
virtual bool operator()(const Input& input) const = 0;
template<typename Lhs, typename Rhs>
constexpr Derivation1<Input, Lhs, Rhs> operator||(const Lhs& left, const Rhs& right) const;
template<typename Lhs, typename Rhs>
constexpr Derivation2<Input, Lhs, Rhs> operator&&(const Lhs& left, const Rhs& right) const;
};
以及它的 2 个派生:
template<typename Input, typename... TSpecialized>
class Derivation1 : public Base<Input>
{
public:
bool operator()(const Input& input) const override
{
// ...
}
};
template<typename Input, typename... TSpecialized>
class Derivation2 : public Base<Input>
{
public:
bool operator()(const Input& input) const override
{
// ...
}
};
以及我们在基类中声明的运算符的定义:
template <typename Input>
template <typename Lhs, typename Rhs>
constexpr Derivation1<Input, Lhs, Rhs> Base<Input>::operator||(const Lhs& left, const Rhs& right) const
{
return Derivation1<Input, Lhs, Rhs>();
}
template <typename Input>
template <typename Lhs, typename Rhs>
constexpr Derivation2<Input, Lhs, Rhs> Base<Input>::operator&&(const Lhs& left, const Rhs& right) const
{
return Derivation2<Input, Lhs, Rhs>();
}
类型 Rhs 和 Lhs 也是基类的派生。
当我尝试使用这样的运算符时:
Derivation3<int, 10, 20> left;
Derivation4<int, 300, 290> right;
auto result = left || right;
我收到一个错误,说运算符的重载与参数不匹配。两种派生具有相同的基本类型:Base<int>
,其中应声明重载。变量 result
应该是 Derivation1
类型(就像我们在上面的代码中声明的那样)。
在这种情况下,如何正确重载操作?
我找到了解决方案。我在基类中创建了一个 typedef:
template<typename Input>
class Base
{
public:
virtual ~Base() = default;
virtual bool operator()(const Input& input) const = 0;
typedef Input inputType;
};
我将运算符重载移到了类之外:
template <typename Lhs, typename Rhs>
constexpr Derivation1<typename Lhs::inputType, Lhs, Rhs> operator||(const Lhs& left, const Rhs& right)
{
return Derivation1<typename Lhs::inputType, Lhs, Rhs>();
}
template <typename Lhs, typename Rhs>
constexpr Derivation2<typename Lhs::inputType, Lhs, Rhs> operator&&(const Lhs& left, const Rhs& right)
{
return Derivation2<typename Lhs::inputType, Lhs, Rhs>();
}
此代码按预期完美运行。
相关文章:
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 重载运算符new[]的行为取决于析构函数
- 为什么将值返回函数传递给重载=运算符对运算符函数有效,而对其他运算符无效
- 在 myVector 类中重载运算符 + 时出错
- 为什么常量词在重载运算符中不与 ostream 对象一起使用<<?
- 如何在 cpp 中重载运算符 +=?
- C++ 如何重载 [] 运算符并进行函数调用
- 重载运算符的范围是什么?它是否会影响作为类成员的集合的插入函数?
- 为什么我可以在不重载 "=" 运算符的情况下将一个对象分配给另一个对象?
- 重载运算符有地址吗?
- 如何迭代重载运算符 [] 的类?
- 重载运算符与添加问题
- 模板基类中的重载运算符
- 如何调用用于重载运算符"<<"的 friend 函数?
- 在 C++17 中的命名空间和子命名空间中重载运算符是不明确的
- 重载运算符<<采用谷歌 C++ 风格
- C++ 如何正确重载 + 运算符
- cout (<<) 重载运算符不打印减去的矩阵
- 如何在 c++ 中重载运算符 + 以便能够 whrite c_str = "smth" + c_str;
- 重载运算符*以获取对另一个类的实例的引用