www.digitalmars.com         C & C++   DMDScript  

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

reply Vitaliy Fadeev <vital.fadeev gmail.com> writes:
Detect 8-bit alligned type TXY by TX,TY.

```d
     union
     {
         struct
         {
             TX x;
             TY y;
         }
         TXY xy;
     }

     alias TXY = Detect!(TX,TY);
```

Wanted:

```d
Detect( uint, uint )     == ulong;
Detect( uint, ubyte )    == ulong;
Detect( ushort, ushort ) == uint;
Detect( ushort, ubyte )  == uint;
```

Is there a D-trait or D-function for detect TXY (named for 
example ```Detect``` in code above) ?
Sep 18 2023
parent reply "Richard (Rikki) Andrew Cattermole" <richard cattermole.co.nz> writes:
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).
Sep 18 2023
parent reply Vitaliy Fadeev <vital.fadeev gmail.com> writes:
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" ); } ```
Sep 18 2023
parent Imperatorn <johan_forsberg_86 hotmail.com> writes:
On Tuesday, 19 September 2023 at 06:41:49 UTC, Vitaliy Fadeev 
wrote:
 On Tuesday, 19 September 2023 at 06:33:25 UTC, Richard (Rikki) 
 Andrew Cattermole wrote:
 [...]
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" ); } ```
Do a mixin
Sep 21 2023