On Fri, Aug 29, 2025 at 10:37 AM Jakub Jelinek <[email protected]> wrote:
> On Fri, Aug 29, 2025 at 10:08:38AM +0200, Tomasz Kaminski wrote:
> > > + if (sizeof(S) != sizeof(T))
> > > + return;
> > >
> > These seem insufficient to prevent compilation errors that
> bit_cast<T>(S{})
> > would
> > produce if their size is not much. My preference here would be to have
> some
> > dr-require
> > expressing that, but I am not sure if it is possible.
>
> It can be done also just with
> #if __SIZEOF_INT__ * 2 == _SIZEOF_LONG_LONG__
> because I'm not aware of any target supported by GCC that would insert
> any padding in these structures.
>
Yes, that is even better. C++ made me biased towards not using
preprocessing,
so I miss simple solutions.
>
> > As alternative, you could use:
> > template<typename X, typename Y>
> > void test01() {} // empty
> > template<typename X, typename Y>
> > requires (sizeof(X) == sizeof(Y))
> > void test01() {
> > }
> > And call it test01<S, T>.
> >
> > However, I think best option would be to change S, to store:
> > struct S { alignas(long) char a[sizeof(long)]; }
> > You are guaranteed to have at least 4 bytes there, so you can set them,
> > instead of setting members.
>
> I'd prefer both not using char because that has different behavior for
> TBAA.
> Which is why I think 2xint vs. long long is better.
>
> Jakub
>
>