I want to generate class Y
from class X
:
class X{
T e;
optional<T1> e1;
optional<T2> e2;
optional<T3> e3;
T4 e4;
// Member functions
...
};
class Y{
T e;
T1 e1;
T2 e2;
T3 e3;
T4 e4;
bool b1;
bool b2;
bool b3;
// Member functions
...
}
So, basically I want to put the booleans of optional at the end to win some memory, (this is called packing) You can assume that the member functions doesn't use any optional, so the generated code would be correct, also the class can have as many optionals as possible
I know that there is no class reflexion in C++, but with Variadic Templates, it seems we can do some awsome stuff.
I have been thinking about a solution for this issue using them , but I couldn't find any. I thought someonse else may have tried something like this.
EDIT :
The main goal of this is to reduce the memory of the first class, by putting the booleans of optional at the end (reducing padding), derivation won't do this, since the size
of the derived class
would be greater than base class
The code would be generated either by passing the class X as template to a generator ClassGenerator<X>
, or maybe the generator can take more arguments like this ClassGenerator<X, T1, T2, T3>
The best way to reclaim memory is to declare them as bitfields, or to do equivalent bitwise arithmetic manually.
The problem can be solved by using variadic templates and bitwise arithmetic. To aggregate the members you can use
std::tuple
, and to aggregate the bits usestd::bitset
. From there, you can make an accessor that returns anoptional
by value.There are no member functions there.
You might be overestimating the freedom that C++ gives you in typecasting. You cannot cast
X*
toY*
, for example. Not by a long shot. (At least, not if you use theY*
result.)Really, the hard part is that you don't have
optional
any more and you need to reinvent it. So then you need some kind of union, but individually the unions don't know how to move or destroy themselves, because they need that bit. So you can't put them inside atuple
, so then you need to reinventtuple
as well.