GCC support for extensions from later standards
Nikolas Klauser
nikolasklauser@berlin.de
Tue Aug 8 16:33:56 GMT 2023
Yes, Clang allows it. Most importantly though: you don’t have to be in a constexpr function for `if constexpr` to be useful. e.g. the Algorithms aren’t constexpr until C++20, but a lot of them have overloads for improving the algorithm based on the iterator category. Having `if constexpr` there instead of enable_ifs seems like quite a nice improvement to me. The main problem is probably that libc++ back-ports a lot of the C++11 stuff to C++03, so in these cases `if constexpr` won’t help.
> On Aug 8, 2023, at 9:10 AM, Jonathan Wakely <jwakely.gcc@gmail.com> wrote:
>
> 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 | }
> | ^
More information about the Libstdc++
mailing list