簡體   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