首页 \ 问答 \ C ++模板元编程:重载运算符(C++ template meta programming: overloading operators)

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 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?


原文:https://stackoverflow.com/questions/41626239
更新时间:2022-01-10 09:01

相关文章

更多

最新问答

更多
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
  • 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
  • 以编程方式创建UITableView(Creating a UITableView Programmatically)
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
  • Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
  • 设置kotlin扩展名(Setting a kotlin extension)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 如何在vim中启用python3?(How to enable python3 in vim?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • dedecms如何安装?
  • 在哪儿学计算机最好?
  • 学php哪个的书 最好,本人菜鸟
  • 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
  • 如何覆盖错误堆栈getter(How to override Error stack getter)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
  • 电脑高中毕业学习去哪里培训
  • 正则表达式验证SMTP响应(Regex to validate SMTP Responses)