C++ Logo


Advanced search

constexpr ternary operator

From: Hui Xie <hui.xie1990_at_[hidden]>
Date: Thu, 21 Oct 2021 13:48:48 +0100

Sometimes I have this situation

template <typename T>
void f(T const& t){
    const auto obj = []{
           if constexpr (pred<T>){
                return makeFoo();
           } else {
                return makeBar();

    const auto x = g(obj); // different overload g for Foo and Bar
    // more code

What I really want to write is a ternary operator
const auto obj = pred<T> ? makeFoo() : makeBar();

But Foo and Bar are different types, so it won't work.

What about allowing us to write
const auto obj = pred<T> constexpr? makeFoo() : makeBar();

Any ideas?


Received on 2021-10-21 07:49:16