On Tue, 8 Aug 2023 at 17:07, Jonathan Wakely wrote:
>
> On Tue, 8 Aug 2023 at 17:04, Nikolas Klauser wrote:
> >
> > Luckily most of these aren’t problems for libc++. We only support the
> > latest GCC. We can only use `if constexpr` in C++11, but that is already a
> > win I think.
>
> Can you use it in C++11 though? The body of a constexpr function must
> be a single return statement, so if-constexpr isn't allowed.
Clang allows it with multiple warnings:
ifc.cc:3:6: warning: constexpr if is a C++17 extension [-Wc++17-extensions]
if constexpr (sizeof(i) >= 4)
^
ifc.cc:3:3: warning: use of this statement in a constexpr function is
a C++14 extension [-Wc++14-extensions]
if constexpr (sizeof(i) >= 4)
^
ifc.cc:8:5: warning: multiple return statements in constexpr function
is a C++14 extension [-Wc++14-extensions]
return 0;
^
ifc.cc:5:5: note: previous return statement is here
return i << 3;
^
But GCC gives a warning for if-constexpr and then an error for the
invalid function body:
ifc.cc: In function 'constexpr int f(int)':
ifc.cc:3:6: warning: 'if constexpr' only available with '-std=c++17'
or '-std=gnu++17' [-Wc++17-extensions]
3 | if constexpr (sizeof(i) >= 4)
| ^~~~~~~~~
ifc.cc:9:1: error: body of 'constexpr' function 'constexpr int f(int)'
not a return-statement
9 | }
| ^