public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Nikolas Klauser <nikolasklauser@berlin.de>
To: Jonathan Wakely <jwakely.gcc@gmail.com>
Cc: Jakub Jelinek <jakub@redhat.com>,
	Jason Merrill <jason@redhat.com>,
	gcc@gcc.gnu.org, Louis Dionne <ldionne.2@gmail.com>,
	Mark de Wever <koraq@xs4all.nl>,
	aaron@aaronballman.com, libstdc++ <libstdc++@gcc.gnu.org>
Subject: Re: GCC support for extensions from later standards
Date: Tue, 8 Aug 2023 09:33:56 -0700	[thread overview]
Message-ID: <675C38C8-3AA7-4974-BBB1-88ED8BBEB794@berlin.de> (raw)
In-Reply-To: <CAH6eHdSC6_SQdZT3GhwO4GPq3CuHN2M_fSfRu9mKk6YiTVZ+sg@mail.gmail.com>

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 | }
>     | ^


      reply	other threads:[~2023-08-08 16:34 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-02 19:01 Nikolas Klauser
2023-08-06 19:19 ` Jason Merrill
2023-08-06 19:43   ` Jonathan Wakely
2023-08-07 13:04     ` Jonathan Wakely
2023-08-08  8:55       ` Jonathan Wakely
2023-08-08  3:03   ` Nikolas Klauser
2023-08-08  7:33     ` Jakub Jelinek
2023-08-08 16:03       ` Nikolas Klauser
2023-08-08 16:07         ` Jonathan Wakely
2023-08-08 16:10           ` Jonathan Wakely
2023-08-08 16:33             ` Nikolas Klauser [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=675C38C8-3AA7-4974-BBB1-88ED8BBEB794@berlin.de \
    --to=nikolasklauser@berlin.de \
    --cc=aaron@aaronballman.com \
    --cc=gcc@gcc.gnu.org \
    --cc=jakub@redhat.com \
    --cc=jason@redhat.com \
    --cc=jwakely.gcc@gmail.com \
    --cc=koraq@xs4all.nl \
    --cc=ldionne.2@gmail.com \
    --cc=libstdc++@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).