是否可以将一组给定的运算符简化为任意类集

是否可以将一组给定的运算符简化为任意类集

本文介绍了是否可以将一组给定的运算符简化为任意类集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下类集及其操作符的关系:我们可以通过两种不同的方式来实现它们.第一个是在类内定义运算符,第二个是在类外定义运算符...

Consider the following set of classes and the relationship of their operators: We can implement them in two distinct ways. The first where the operators are defined within the class, and the latter where they are defined outside of the class...

template<typename T>
struct A {
    T value;

    T& operator+(const A<T>& other) { return value + other.value; }
    // other operators
};

temlate<typename T>
struct B {
   T value;

   T& operator+(const B<T>& other) { return value + other.value; }
};

// Or...
template<typename T>
struct A {
    T value;
};
template<typename T>
T& operator+(const A<T>& lhs, const A<T>& rhs) { return lhs.value + rhs.value; }
// ... other operators

template<typename T>
struct B {
    T value;
};
template<typename T>
T& operator+(const B<T>& lhs, const B<T>& rhs) { return lhs.value + rhs.value; }
// ... other operators

在C ++中,有什么方法可以使操作符成为单个类或结构,使我可以在任意类C中简单地声明或定义它们,而不必为这些操作符多次编写相同的操作符每个课?我假设对于运算符,每个定义它们的不同类都将具有相同的行为和属性,考虑到它们都将遵循相同的模式.

Is there any way in C++ where I would be able to make a single class or struct of operators to where I could simply be able to declare or define them within any arbitrary class C without having to write those same operators multiple times for each class? I'm assuming that the operators will have the same behavior and property for each distinct class that defines them considering that they will all follow the same pattern.

例如:

template<typename T, class Obj>
struct my_operators {
    // define them here
};

// Then
template<typename T>
struct A {
    T value;
    my_operators ops;
};

template<typename T>
struct B {
    T value;
    my_operators ops;
};

请记住,我将其限制为C ++ 17,因为我无法使用诸如Concepts之类的任何C ++ 20功能.如果可能的话,我将能够使用哪种方法或构造使用时,其结构和正确的语法是什么样的?如果可以的话,只要使用类的模式匹配,我就可以编写一次运算符,然后重用它们,而不必为每个单独的类编写这些运算符...

Remember I'm restricting this to C++17 as I'm not able to use any C++20 features such as Concepts... If this is possible, what kind of method or construct would I be able to use, what would its structure and proper syntax look like? If this is possible then I'd be able to write the operators once and just reuse them as long as the pattern of the using classes matches without having to write those operators for each and every individual class...

推荐答案

使用CRTP继承怎么样?

What about using CRTP inheritance?

#include <iostream>

template <typename T>
struct base_op
 {
   auto operator+ (T const & o) const
    { return static_cast<T&>(*this).value + o.value; }
 };

template<typename T>
struct A : public base_op<A<T>>
 { T value; };

template<typename T>
struct B : public base_op<B<T>>
 { T value; };

int main()
 {
   A<int>   a1, a2;
   B<long>  b1, b2;

   a1.value = 1;
   a2.value = 2;

   std::cout << a1+a2 << std::endl;

   b1.value = 3l;
   b2.value = 5l;

   std::cout << b1+b2 << std::endl;
 }

显然,这仅适用于具有 value 成员的模板类.

Obviously this works only for template classes with a value member.

对于课外"版本, base_op 变为

template <typename T>
struct base_op
 {
   friend auto operator+ (T const & t1, T const & t2)
    { return t1.value + t2.value; }
 };

-编辑-

OP询问

这有点复杂,因为它们必须返回对派生对象的引用...我想(例如)在 base_op 内的 operator + =(),可能是

It's a little more complicated because they must return a reference to the derived object... I suppose that (for example) operator+=(), inside base_op, could be something as

T & operator+= (T const & o)
 {
   static_cast<T&>(*this).value += o.value;

   return static_cast<T&>(*this);
 }

这篇关于是否可以将一组给定的运算符简化为任意类集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 07:37