Class instance alignment

tsbockman thomas.bockman at gmail.com
Fri Feb 19 23:53:55 UTC 2021


How can I get the alignment of a class instance? I know how to 
get the size:
     __traits(classInstanceSize, T)
But, there doesn't appear to be any equivalent trait for the 
alignment.

(Knowing the alignment of a class instance is required to 
correctly use core.lifetime.emplace, or do any sort of manual 
allocation of class instances. I know that I can conservatively 
estimate the alignment as nextPow2(__traits(classInstanceSize, 
T)), but that is nearly always a wasteful over-estimate.

I've come across others' code online that simply assumes the 
alignment is size_t.alignof, but that's not right either because 
a class instance may contain SIMD vectors with higher alignments, 
or it may be aligned to a cache line size for efficient 
multi-threaded access.)


More information about the Digitalmars-d-learn mailing list