I have some different types of data that I want to be able to add to an object (here called "A" and "B"). If I add them in the same order for two different objects, copying one to the other works fine (eg A<B<Point> > abPoint1; A<B<Point> > abPoint2 = abPoint1;
). However, if I add them in different orders (eg A<B<Point> > abPoint; B<A<Point> > baPoint = abPoint; // compiler error
) because the type signature is not the same. Is there any way to do this without handling an exponential number of mixin combinations explicitly?
Here is a MWE for testing:
// 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;
}
And even better, is there a way to only copy "available" pieces of the data? That is:
A<B<C<D<Point>>>> abcdPoint;
A<C<Point>> acPoint;
abcdPoint = acPoint;
would only copy the members from A and C.
In order to be able show what I think the answer to be without really testing to see if it works, I will call this an answer.
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> > {};
I fleshed out your example the best I could. Copying a abPoint into a baPoint works.
#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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.