C++ Logo

std-proposals

Advanced search

Re: [std-proposals] if () {typedef x y;}

From: Marcin Jaczewski <marcinjaczewski86_at_[hidden]>
Date: Mon, 2 May 2022 12:47:06 +0200
niedz., 1 maj 2022 o 23:10 Thiago Macieira via Std-Proposals
<std-proposals_at_[hidden]> napisaƂ(a):
>
> On Sunday, 1 May 2022 01:34:01 PDT Wilhelm M via Std-Proposals wrote:
> > Isn't this std::conditional?
> >
> >
> > typedef std::conditional<cond, int, double> type;
> >
> > https://en.cppreference.com/w/cpp/types/conditional
>
> Another trick is with auto and lambda:
>
> auto c = []() {
> if constexpr (cond)
> return 1;
> else
> return 1.0;
> };
> // c is either int or double, depending on the condition.
>
You forget the last `();` to have IIFE (aka Immediately Invoked Lambda
Expression).

Btw I think if you use IIFE it is better make it clear you use it
instead of declaring normal expression, like:
```
auto c = ([]{ // first `(` show that is something strange there, some
simply add `std::invoke` to be fully clear.
 ...
})();
```



> --
> Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
> Software Architect - Intel DPG Cloud Engineering
>
>
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2022-05-02 10:47:16