public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "ppalka at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/102065] [C++20] Substitution failure of function instantiates its argument
Date: Wed, 25 Aug 2021 14:44:44 +0000	[thread overview]
Message-ID: <bug-102065-4-IaYXsZEFjL@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-102065-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102065

Patrick Palka <ppalka at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |DUPLICATE
                 CC|                            |ppalka at gcc dot gnu.org
             Status|UNCONFIRMED                 |RESOLVED

--- Comment #1 from Patrick Palka <ppalka at gcc dot gnu.org> ---
Thanks for the bug report!  Looks like this apparent regression started with
r11-2774 (which implements the changes in wg21.link/cwg2369).

But I think GCC's behavior is correct here, according to the latest standard. 
The associated constraints of a function template are checked immediately after
deducing all template arguments, rather than after substituting the deduced
template arguments into the function type, so your use of enable_if_t as a
function parameter doesn't actually prevent the std::invocable constraint from
getting checked (and for the hard error to occur when this constraint check
causes the body of the lambda to get instantiated with int*).

One work around in your case is to give the lambda a non-deduced return type of
'void' so that we don't need to instantiate the body of the lambda during the
std::invocable check:

        [](auto e) -> void
        {
            elem_func(e);
        }

Another more general workaround is to instead encode the enable_if_t test as an
additional constraint that guards the std::invocable constraint, something
like:

#include <functional>

void elem_func(int) {}

template <
    typename Sequence,
    typename SequenceHandler
>
  requires std::is_pointer_v<Sequence> && std::invocable<SequenceHandler, int>
void func(
    const Sequence& values,
    SequenceHandler&& handler)
{
}

template <
    typename T,
    typename ValueHandler
>
  requires (!std::is_pointer_v<T>) && std::invocable<ValueHandler, T>
void func(
    T value,
    ValueHandler&& handler)
{
}

int main()
{
    func(
        new int[1], // pointer => 1st func should be called 
        [](auto e)  // lambda should be std::invocable<int> => decltype(e)==int
        {
            elem_func(e); // cannot call elem_func if decltype(e)==int* (2nd
func)
        }
    );
}

*** This bug has been marked as a duplicate of bug 99599 ***

  reply	other threads:[~2021-08-25 14:44 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-25 13:36 [Bug c++/102065] New: " seredinyegor at gmail dot com
2021-08-25 14:44 ` ppalka at gcc dot gnu.org [this message]
2021-08-25 18:00 ` [Bug c++/102065] " seredinyegor at gmail dot com

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=bug-102065-4-IaYXsZEFjL@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@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).