digitalmars.D.learn - Detect 8-bit alligned type TXY by TX,TY.
- Vitaliy Fadeev (22/22) Sep 18 2023 Detect 8-bit alligned type TXY by TX,TY.
- Richard (Rikki) Andrew Cattermole (7/7) Sep 18 2023 I assume what you are wanting is to get the alignment for a given type?
- Vitaliy Fadeev (28/37) Sep 18 2023 Thank, Richard.
- Imperatorn (3/32) Sep 21 2023 Do a mixin
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
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
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
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:Do a mixin[...]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 21 2023