On Thu, 12 Jul 2018 at 19:01, Pedro Alves <pal...@redhat.com> wrote:
>
> On 07/12/2018 05:17 PM, Richard Sandiford wrote:
> > Pedro Alves <pal...@redhat.com> writes:
>
> >> (an
> >>> alternative to pointers is to return a struct with the wide int result
> >>> and the overflow flag),
> >>
> >> +1.  I've been pushing GDB in that direction whenever possible.
> >
> > I agree that can sometimes be better.  I guess it depends on the
> > context.  If a function returns a bool and some other data that has no
> > obvious "failure" value, it can be easier to write chained conditions if
> > the function returns the bool and provides the other data via an output
> > parameter.
>
> I agree it depends on context, though your example sounds like a
> case for std::optional<T>.  (We have gdb::optional<T> in gdb, since
> std::optional is C++17 and gdb requires C++11.  LLVM has a similar
> type, I believe.)

Yes I agree.

optional<T> foo ();
void bar (const T&);

if (optional<T> val = foo ())
  bar (*val);

Or expected<T> which is a union of T and an error flag (and a
discriminator value to tell you which union member is active). That
also allows chaining in a similar way. Forgetting to test whether the
optional/expected holds a value before trying to use that value would
be an ICE.

Reply via email to