vendredi 15 octobre 2021

"Merge" PODs into one

Is there a way to compose (or merge, aggregate) PODs between them ? One intuitive solution:

struct Base1 { int i; };
struct Base2 { char c; };

struct Derived : Base1, Base2 {};

// in a more generalized way
// template <class... Ts> struct Aggregate : Ts... {};

Except, we lose something:

int main()
{
    Derived d {42, 'c'}; // OK
    auto [i, c] = d; // Error
    static_assert(std::is_standard_layout_v<Derived>); // Error
}

I see we could end up with some ambiguousities, conflicts, between the merged base classes. But it would be pretty nice to merge PODs into one. The result I try to achieve:

struct Expected { int i; char c; }; // I want Derived to behave exactly like this

Am I in the realm of reflection? Should I end up using macros?





Aucun commentaire:

Enregistrer un commentaire