C++ Logo

std-proposals

Advanced search

Re: [std-proposals] std::elide

From: Thiago Macieira <thiago_at_[hidden]>
Date: Sat, 25 May 2024 13:36:28 -0300
On Saturday 25 May 2024 13:16:11 GMT-3 Frederick Virchanza Gotham via Std-
Proposals wrote:
> mutex Func(void)
> {
> struct S {
> mutex m;
> S() { m.lock(); }
> };
> static_assert( sizeof (S) == sizeof (mutex) );
> static_assert( alignof(S) == alignof(mutex) );
> constexpr auto pf1 = +[](){ return S(); };
> auto const pf2 = *static_cast<mutex(*const
> *)()>(static_cast<void const*>(&pf1));
> return pf2();
> }
>
> Then she'd say to me, "Isn't it undefined behaviour to invoke that
> lambda after you've casted it?", and I'd respond "I won't tell anyone
> if you don't".

There's an easy way to fix the UB: return an S. You can also just make S
inherit from mutex.

This is the type of alternative solution that needs to be explored in the
paper. We shouldn't jump at solutions without investigating what else is there
and whether the need is real.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Principal Engineer - Intel DCAI Fleet Engineering and Quality

Received on 2024-05-25 16:36:32