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
类型(就像我们在上面的代码中声明的那样)。在这种情况下,如何正确地重载operatros?
I'm currently playing around with template metaprogramming. I'm trying to make a finite state machine by using tmp. I know that there are several implementations in the web but I want to implement one by myself as an exercise.
I'm trying to overload operators for templated derivatives of a templated base class inside the base class. Assume we have a base class:
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; };
and 2 of its derivations:
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 { // ... } };
and the definition of the operators that we declared in the base class:
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>(); }
The types Rhs and Lhs are derivations of the base class aswell.
When I try to use the operators like:
Derivation3<int, 10, 20> left; Derivation4<int, 300, 290> right; auto result = left || right;
I'm getting an error that says no overload of the operator matches the arguments. Both derivations have the same base type:
Base<int>
in which the overloads should be declared. The variableresult
should then be of typeDerivation1
(like we declared it in the code above).How do I overload the operatros properly in this case?
原文:https://stackoverflow.com/questions/41626239