Thread overview
Detect 8-bit alligned type TXY by TX,TY.
September 19

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

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

    alias TXY = Detect!(TX,TY);

Wanted:

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) ?

September 19
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).
September 19

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:

// 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" );
}
September 21

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:

// 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