C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Try-catch declaration

From: Giuseppe D'Angelo <giuseppe.dangelo_at_[hidden]>
Date: Wed, 22 Nov 2023 16:14:06 +0100
Il 22/11/23 14:51, Lénárd Szolnoki via Std-Proposals ha scritto:
> int f() {
> decltype(&func()) ptr = nullptr;
> try {
> ptr = &func();
> } catch(...) {}
>
> // use ptr here
> }
>
> I'm strongly against this proposal, as it introduces the concept of
> "uninitialized reference" that didn't exist before. Sure references can
> become stale and thus invalid, but they are not invalid right after
> their initialisation. This is essentially a null reference.
>
> The pointer makes it explicit that it might have a state other than
> referring to an object, and therefore safer. It also makes it possible
> (but not necessary) to check for this state.

Or -- hear me out --

   std::optional<decltype(func())> opt; // i.e. possibly optional<X&>

   try {
     opt = func();
   } catch (...) {}

   use(*opt);

--
In general, the proposed syntax isn't consistent with everything else in 
C++. `if`, `for`, etc. statements always limit the visibility of 
declarations in their guards to inner block. Why should `try` extend it 
further?
-- 
Giuseppe D'Angelo

Received on 2023-11-22 15:14:10