I have a class to wrap POD struct types, but I’m having trouble when a POD struct has another POD inside, because my wrapper class only stores the POD as it is, using composition.
#include <utility>
template <typename T>
class POD {
protected:
T pod_;
public:
POD(T&& pod)
: pod_{std::move(pod)}
{
}
POD(const T& pod)
: pod_{pod}
{
}
const T& cpod() const { return pod_;}
};
extern "C" {
typedef struct {
float x;
float y;
} v2f_t;
typedef struct {
v2f_t start;
v2f_t end;
int foo;
int bar;
} line_t;
}
// example usage
class Vector2f: public POD<v2f_t> {
public:
Vector2f(v2f_t&& pod)
: POD<v2f_t>(std::move(pod))
{}
Vector2f(const v2f_t& pod)
: POD<v2f_t>(pod)
{}
float sum() {
return pod_.x + pod_.y;
}
};
class Line: public POD<line_t> {
Line(line_t&& pod)
: POD<line_t>(std::move(pod))
{}
float total_sum() {
// I would like to call
// start.sum() + end.sum();
// but start and end are of type v2f_t so
// I need to create some kind of temporal POD wrappers
return Vector2f(pod_.start).sum() + Vector2f(pod_.end).sum();
}
}
Is there some to tell the compiler that one POD has another POD inside and change the POD wrapper class to have some kind of template parameter with the inner POD?
// something like
class Line: public POD<line_t, Vector2f> {
// members would now be something similar to
Vector2f start;
Vector2f end;
int foo;
int bar;
}