C++ Logo

std-proposals

Advanced search

Re: [std-proposals] The Lakos rule, undefined behaviour and noexcept

From: Thiago Macieira <thiago_at_[hidden]>
Date: Fri, 25 Sep 2026 08:48:09 -0700
On Friday, 25 September 2026 08:09:59 Pacific Daylight Time Frederick Virchanza
Gotham via Std-Proposals wrote:
> "Where the ISO C++ Standard says that a particular thing results
> in undefined behaviour, is it okay for a particular compiler to say
> that the behaviour is well-defined and that the entire program must
> continue to function as normal?"

Yes, it's perfectly fine for a compiler to define some behaviour and even
document it. Why it may want to do that is out of scope.

The important thing is that it does not make the program portable to other
compilers. There is no guarantee others will adopt the behaviour.

A good example of this is the old union punning trick. In C++, this (used to
be) undefined:

union {
    float f;
    uint32_t u;
} u;
u.f = getsomefloat();
return u.u;

For a long time, GCC implemented this as completely defined behaviour. We had a
case of this in QMutexLocker back in Qt 4 days, with a pointer-sized integer
punning a pointer to the mutex - we'd use the LSB to indicate whether we'd
locked the mutex. We'd never had a problem with GCC and most other compilers
like MSVC. However, the Sun Studio compiler did not adopt the behaviour, so it
proceeded to eliminate some modifications of the inactive member. As a result,
we either failed to unlock a mutex that we had locked, or we crashed in
attempting to do so because we the instruction resetting the LSB had been
eliminated.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Principal Engineer - Intel Data Center - Platform & Sys. Eng.

Received on 2026-09-25 15:48:22