C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Lakos rule

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Thu, 21 Dec 2023 12:50:32 -0500
On Thu, Dec 21, 2023 at 12:41 PM Frederick Virchanza Gotham via
Std-Proposals <std-proposals_at_[hidden]> wrote:
> On Thursday, December 21, 2023, Jason McKesson via Std-Proposals wrote:
>>
>>
>> If the "throws: nothing" functions actually do throw (violating the standard, BTW),
>
>
>
> Let's say I'm writing a C++ compiler for my new 128-Bit germanium-based supercooled microcontroller, and I want to implement 'std::align' whose prototype is as follows:
>
> void *align( std::size_t alignment,
> std::size_t size,
> void*& ptr,
> std::size_t& space );
>
> This function does not have 'noexcept' written after it, even though it's not supposed to throw. Let's say my first line in the body of this function is:
>
> if ( nullptr == p ) throw std::runtime_error("Cannot align a nullptr");
>
> Is my implementation of 'std::align' in violation of the Standard? If so, then is the following statement true?
> "The C++ Standard does not mark 'std::align' as 'noexcept' in order to accommodate implementations that violate the Standard".

`std::align` has a precondition that `ptr` must represent an address
of at least `space` bytes. `nullptr` doesn't represent an address.
Therefore, if `ptr` is ever `nullptr`, the caller has invoked UB and
it *does not matter* if the function throws.

Put simply, the caller violated the standard *first*.

When I said that it doesn't throw, I meant it doesn't have
well-defined behavior for throwing. What happens if behavior is
undefined is irrelevant because... it's undefined.

Received on 2023-12-21 17:50:42