Date: Mon, 4 Aug 2025 01:06:46 +0300
On 3 Aug 2025 22:19, Frederick Virchanza Gotham via Std-Proposals wrote:
>
> On Sunday, August 3, 2025, David Brown wrote:
>
> What use would that be? If the programmer knows that a pointer in
> the function might be a null pointer, and wants to throw if that is
> the case, then the programmer can write "if (!p) throw nullptr;", or
> whatever suits their needs.
>
> It would allow us to write one-liner functions like:
>
> unsigned GetCOMportNumber(char const *const p)
> {
> return stoul( strstr(p, "COM") + 3 );
> }
No, it wouldn't, because C functions don't throw. Even if they did,
existing functions would not change to start throwing.
Besides, adding 3 to the result of strstr is a potential UB anyway.
> and also to write a one-liner like:
>
> pInterface1->GetInterface2()->LastChild()->Release();
>
> instead of:
>
> if ( nullptr == pInterface1 ) return;
> auto p = pInterface1->GetInterface2();
> if ( nullptr == p ) return;
> auto p2 = p->LastChild();
> if ( nullptr == p2 ) return;
> p2->Release();
Switch your code to a smart pointer with a checked operator-> and
operator* and there you go. Along with the associated performance penalties.
>
> On Sunday, August 3, 2025, David Brown wrote:
>
> What use would that be? If the programmer knows that a pointer in
> the function might be a null pointer, and wants to throw if that is
> the case, then the programmer can write "if (!p) throw nullptr;", or
> whatever suits their needs.
>
> It would allow us to write one-liner functions like:
>
> unsigned GetCOMportNumber(char const *const p)
> {
> return stoul( strstr(p, "COM") + 3 );
> }
No, it wouldn't, because C functions don't throw. Even if they did,
existing functions would not change to start throwing.
Besides, adding 3 to the result of strstr is a potential UB anyway.
> and also to write a one-liner like:
>
> pInterface1->GetInterface2()->LastChild()->Release();
>
> instead of:
>
> if ( nullptr == pInterface1 ) return;
> auto p = pInterface1->GetInterface2();
> if ( nullptr == p ) return;
> auto p2 = p->LastChild();
> if ( nullptr == p2 ) return;
> p2->Release();
Switch your code to a smart pointer with a checked operator-> and
operator* and there you go. Along with the associated performance penalties.
Received on 2025-08-03 22:06:53