简体   繁体   中英

C++ template meta programming: overloading operators

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 variable result should then be of type Derivation1 (like we declared it in the code above).

How do I overload the operatros properly in this case?

I found a solution. I've created a typedef in the base class:

template<typename Input>
class Base
{
public:
    virtual ~Base() = default;    
    virtual bool operator()(const Input& input) const = 0;

    typedef Input inputType;
};

and I moved the operator overloading outside of the class:

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>();
}

This code works perfectly as intended.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM