C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Try-catch declaration

From: Andrey Semashev <andrey.semashev_at_[hidden]>
Date: Wed, 22 Nov 2023 17:55:29 +0300
How would it be better than this?

  decltype(func())* ptr = nullptr;
  try {
    ptr = &func();
  } catch (...) {
    return -1
  }

  // use ptr here

Obviously, you could spell the `ptr` type explicitly instead of using
`decltype`.

I don't think there is a case of an "uninitialized reference" in the
current standard, and your proposed change would introduce it. I don't
see this as a good thing, especially given that a close equivalent code
is possible with the current language.

On 11/22/23 14:51, sasho648 via Std-Proposals wrote:
> So yeah it would be UB to use `ref` if exception is thrown.
>
> On Wed, Nov 22, 2023 at 1:50 PM sasho648 <sasho648_at_[hidden]
> <mailto:sasho648_at_[hidden]>> wrote:
>
> Ok use-case I was thinking is actually:
>
> int f() {
>
> try (auto &ref = func()) {
> } catch(...) {
> return -1;
> }
>
> // use ref here
> }
>
> On Wed, Nov 22, 2023 at 1:47 PM Giuseppe D'Angelo via Std-Proposals
> <std-proposals_at_[hidden]
> <mailto:std-proposals_at_[hidden]>> wrote:
>
> Il 22/11/23 12:31, sasho648 via Std-Proposals ha scritto:
> >
> > So in my experience the biggest issue I had with using
> try-catch is for
> > example when I try to construct a reference within try-catch -
> in order
> > to capture only the exception that could occur there:
> >
> > try {
> > auto &ref = func();
> > }
> > catch(...) {
> > }
> >
> > Problem here is:
> >
> > You either have to use reference wrapper or something like
> that if you
> > want to handle only the exception that could potentially be
> thrown by
> > func. And still it would be ugly.
>
> But what should `ref` refer to if `func()` throws?
>
> Thank you,
> --
> Giuseppe D'Angelo
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> <mailto:Std-Proposals_at_[hidden]>
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
> <https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals>
>
>

Received on 2023-11-22 14:55:32