繁体   English   中英

在mixin类型之间复制数据

[英]Copying data between mixin types

我有一些不同类型的数据,我希望能够添加到对象(这里称为“A”和“B”)。 如果我以相同的顺序为两个不同的对象添加它们,将一个复制到另一个工作正常(例如A<B<Point> > abPoint1; A<B<Point> > abPoint2 = abPoint1; )。 但是,如果我以不同的顺序添加它们(例如A<B<Point> > abPoint; B<A<Point> > baPoint = abPoint; // compiler error ),因为类型签名不相同。 如果没有明确处理指数数量的mixin组合,有没有办法做到这一点?

这是一个用于测试的MWE:

// Standard point representation
struct Point
{
    double x,y,z;
};

// A mixin to add an 'A' value to a point
template<class Base>
class A : public Base
{
public:

    double a;
};

// A mixin to add an 'B' value to a point
template<class Base>
class B : public Base
{
public:

    double b;
};

int main()
{
  A<Point> aPoint;

  B<Point> bPoint;

  // A<Point> a2Point = bPoint; // obviously we can't do this

  A<B<Point> > abPoint;
  B<A<Point> > baPoint;

  abPoint = baPoint; // Something like this seems like it should be possible

  return 0;
}

更好的是,有没有办法只复制“可用”的数据? 那是:

A<B<C<D<Point>>>> abcdPoint; 
A<C<Point>> acPoint; 
abcdPoint = acPoint;

只会复制A和C中的成员

为了能够显示我认为答案是没有真正测试,看它是否有效,我会称之为答案。

template<class BASE>
class A {
public:
  A<BASE> operator=(const &A<BASE> right) {
    // does not block all errors, but catches some:
    static_assert( std::is_base_of< A, BASE >::value, "CRTP failure" );
    auto* dis = static_cast<BASE*>(this);
    BASE::operator=(right);
    dis->a = right.a;
    return this;
  }
  double a;
};

template<class BASE>
class B {
public:
  B<BASE> operator=(const &B<BASE> right) {
    // does not block all errors, but catches some:
    static_assert( std::is_base_of< B, BASE >::value, "CRTP failure" );
    auto* dis = static_cast<BASE*>(this);
    dis->b = right.b;
    BASE::operator=(right);
    return this;
  }
  double b;
};


class aPoint: public A<aPoint>, Point {};

class bPoint: public B<bPoint>, Point {};

class abPoint: public B < A<abPoint> > {};

class baPoint: public A < B<baPoint> > {};

我尽力充实你的榜样。 将abPoint复制到baPoint可以正常工作。

#include <iostream>

struct Point
{
public:
    double x,y,z;
};

// A mixin to add an 'A' value to a point
template<class Base>
class A : public Base
{
public:
    A& operator=( const Point& rhs )
    {
        Point::operator=(rhs);
        return *this;
    }

    template <typename T_RHS>
    A& operator=( const T_RHS& rhs )
    {
        Base::operator=(rhs);
        a = rhs.a;
        return *this;
    }

    double a;
};

// A mixin to add an 'B' value to a point
template<class Base>
class B : public Base
{
public:
    B& operator=( const Point& rhs )
    {
        Point::operator=(rhs);
        return *this;
    }

    template <typename T_RHS>
    B& operator=( const T_RHS& rhs )
    {
        Base::operator=(rhs);
        b = rhs.b;
        return *this;
    }

    double b;
};

int main()
{
  Point point;

  A<Point> aPoint;
  aPoint = point;

  B<Point> bPoint;
  bPoint = point;

  B< A<Point> > baPoint;
  A< B<Point> > abPoint;
  abPoint = baPoint;

  // Fails
  //aPoint = bPoint;
  //bPoint = aPoint;

  // This works
  aPoint.Point::operator=(bPoint);
  bPoint.Point::operator=(aPoint);

  return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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