On Saturday, 16 June 2018 at 08:52:20 UTC, DigitalDesigns wrote:
On Saturday, 30 July 2016 at 13:04:56 UTC, Ali Çehreli wrote:
On 07/30/2016 05:47 AM, Jonathan M Davis via
Digitalmars-d-learn wrote:
> I'm writing some serialization code where I need to skip
static variables.
> So, I have a symbol from a struct, and I'd like to test
whether it's static
> or not.
static variables don't have the .offsetof property:
struct S {
int a;
static int b;
}
// Bonus question: should field be alias or string?
template isStaticVar(T, alias field)
{
enum isStaticVar = !__traits(compiles, mixin("T." ~ field
~ ".offsetof"));
}
void main() {
static assert (!isStaticVar!(S, "a"));
static assert (isStaticVar!(S, "b"));
}
Ali
This doesn't work, treats fields in base classes as static.
One way to test if a member is static is if it it doesn't exist
in tupleof... but this only returns the immediate members and
so will fail. I guess one will have to check all base types
too and if it doesn't exist in any of then it should be static.
Why is it so hard to be able to get basic information like if a
type is static or not?
Probably because the symbols don't carry the information. I'd
reckon a compiler addition to __traits() would probably solve
this. Something like getStaticMembers