Detect 8-bit alligned type TXY by TX,TY.

Vitaliy Fadeev vital.fadeev at gmail.com
Tue Sep 19 06:41:49 UTC 2023


On Tuesday, 19 September 2023 at 06:33:25 UTC, Richard (Rikki) 
Andrew Cattermole wrote:
> I assume what you are wanting is to get the alignment for a 
> given type?
>
> https://dlang.org/spec/property.html#alignof
>
> If instead you want the offset of a given field that's 
> different.
>
> ``T.field.offsetof``
>
> https://dlang.org/spec/struct.html#struct_field_properties
>
> Note: alignment cannot be represented by a type, it can only be 
> represented by a number (for instance it could be 3).

Thank, Richard.

```.offsetof...``` mmm...

May be exists some like:

```d
// TXYXY = Detect!(TX,TX)
// Detect!(uint,uint) == ulong
template Detect(TX,TY)
{
     static if ( TX.sizeof + TY.sizeof <= 8 )
         alias Detect = ubyte;
     else
     static if ( TX.sizeof + TY.sizeof <= 16 )
         alias Detect = ushort;
     else
     static if ( TX.sizeof + TY.sizeof <= 32 )
         alias Detect= uint;
     else
     static if ( TX.sizeof + TY.sizeof <= 64 )
         alias Detect= ulong;
     else
     static if ( TX.sizeof + TY.sizeof <= 128 )
         alias Detect= ucent;
     else
         static assert( 0, "Expected size TX+TY <= 128" );
}
```



More information about the Digitalmars-d-learn mailing list