C++ 模板元编程 : overloading operators

标签 c++ operator-overloading template-meta-programming

我目前正在研究模板元编程。我正在尝试使用 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;
};

及其两个派生词:

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

此代码按预期完美运行。

关于C++ 模板元编程 : overloading operators,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41626239/

相关文章:

c++ - InternetOpenUrl 仅在下载整个 HTTP 响应后返回

c++ - constexpr 计算量的实际限制

c++ - 采用两个默认参数的方法

c++ - 在 GDB (C++) 中找到指向单例事后分析的指针

c# - 这是实现 Equals 和相等/不等运算符的好/有效的习惯用法吗?

c++ - 如何创建 operator*(double) 以在左侧和右侧进行乘法运算?

c++ - 有没有办法在 C++17 中创建编译时类型映射以进行类型检查?

c++ - 如果没有 endl,则重载 ostream 运算符段错误

c++ - 使用 make_tuple 方法获取元组和 func 并返回映射元组的最简单方法

c++ - Clang 与 GCC 与 MSVC 中的 SFINAE 和可见性检查——哪个是正确的?