0

According to https://en.cppreference.com/w/cpp/language/statements, I know I can express condition in C++ by at least the following two ways:

  1. An if statement
    if (a) { b } else { c }
    
  2. An expression statement
    a ? b : c
    

And we have constexpr for if

if constexpr (a) { b } else { c }

Since in some area, we can only use expression statement for conditions, to avoid getting a "error: expected expression". So I write

cond ? call_func(), 0 : 0

However, how can we involve constexpr this in expression statement? This is a real problem, consider

template <int FLAG>
void f() {
    SOME_MACRO_ONLY_ALLOW_EXPRS({ FLAG ? call_func(), 0 : 0; })
}

I know I can specialize f for FLAG=1 and FLAG=0. However, it may involves lots of modification.

calvin
  • 2,125
  • 2
  • 21
  • 38
  • ITYM `cond ? (call_func(), 0) : 0` – john Nov 08 '22 at 08:50
  • 1
    There's always the immediate-lambda trick `[&]{ if constexpr(a) return b; else return c; }();`. Both the lambda definition and calling the lambda are expressions. – MSalters Nov 08 '22 at 08:52

0 Answers0