C++ Logo

STD-PROPOSALS

Advanced search

Subject: Re: [std-proposals] Initial Idea of Indirect If Statement
From: Justin Bassett (jbassett271_at_[hidden])
Date: 2019-10-26 18:24:25


This reminds me of pattern matching. I'm not sure if the pattern matching
proposal allows this, though. Rust would allow something like:

if let Some(x) = some_optional {
  ...
}

We propose a new variant of the if statement that fuses a condition
> declaration with an indirection in the true branch. We call it an
> 'indirect if statement'.
>

I find it a bit odd that this only works for unary `operator*`. I want this
to be more general, but I don't know if there's a good alternative without
going for full pattern matching.

An indirect if statement of the form:
>
> if (DECL : EXPR) X else Y;
>
> is equivalent to:
>
> {
> auto&& __c = EXPR;
> if (__c) {
> DECL = *__c;
> X;
> } else {
> Y; // noteworthy: DECL is not in scope here
> }
> }
>

It would sometimes be preferable to forward __c into DECL, but sometimes
not. E.g. if EXPR returned an optional<vector<...>> and DECL wanted to be a
value and not a reference. I don't know what the right choice is.

I do know that I'd use this if it was in the language.

--Justin



STD-PROPOSALS list run by std-proposals-owner@lists.isocpp.org

Standard Proposals Archives on Google Groups