public inbox for gcc-cvs@sourceware.org help / color / mirror / Atom feed
From: Nathaniel Shead <nshead@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org Subject: [gcc r14-9332] c++: Fix template deduction for conversion operators with xobj parameters [PR113629] Date: Wed, 6 Mar 2024 09:49:07 +0000 (GMT) [thread overview] Message-ID: <20240306094907.19D8D385843A@sourceware.org> (raw) https://gcc.gnu.org/g:49d83e963aa453600088380aebd507e172eb80ad commit r14-9332-g49d83e963aa453600088380aebd507e172eb80ad Author: Nathaniel Shead <nathanieloshead@gmail.com> Date: Wed Mar 6 00:43:22 2024 +1100 c++: Fix template deduction for conversion operators with xobj parameters [PR113629] Unification for conversion operators (DEDUCE_CONV) doesn't perform transformations like handling forwarding references. This is correct in general, but not for xobj parameters, which should be handled "normally" for the purposes of deduction: [temp.deduct.conv] only applies to the return type of the conversion function. PR c++/113629 gcc/cp/ChangeLog: * pt.cc (type_unification_real): Only use DEDUCE_CONV for the return type of a conversion function. gcc/testsuite/ChangeLog: * g++.dg/cpp23/explicit-obj-conv-op.C: New test. Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com> Reviewed-by: Jason Merrill <jason@redhat.com> Diff: --- gcc/cp/pt.cc | 12 +++++- gcc/testsuite/g++.dg/cpp23/explicit-obj-conv-op.C | 49 +++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index c4bc54a8fdb..a6e6c804130 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -23312,10 +23312,18 @@ type_unification_real (tree tparms, parameter pack is a non-deduced context. */ continue; + /* [temp.deduct.conv] only applies to the deduction of the return + type, which is always the first argument here. Other arguments + (notably, explicit object parameters) should undergo normal + call-like unification. */ + unification_kind_t kind = strict; + if (strict == DEDUCE_CONV && ia > 0) + kind = DEDUCE_CALL; + arg = args[ia]; ++ia; - if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict, + if (unify_one_argument (tparms, full_targs, parm, arg, subr, kind, explain_p)) return 1; } @@ -23324,6 +23332,8 @@ type_unification_real (tree tparms, && parms != void_list_node && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION) { + gcc_assert (strict != DEDUCE_CONV); + /* Unify the remaining arguments with the pack expansion type. */ tree argvec; tree parmvec = make_tree_vec (1); diff --git a/gcc/testsuite/g++.dg/cpp23/explicit-obj-conv-op.C b/gcc/testsuite/g++.dg/cpp23/explicit-obj-conv-op.C new file mode 100644 index 00000000000..a6ae4ea1dda --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp23/explicit-obj-conv-op.C @@ -0,0 +1,49 @@ +// PR c++/113629 +// { dg-do compile { target c++23 } } + +template <typename T> constexpr bool is_lvalue = false; +template <typename T> constexpr bool is_lvalue<T&> = true; + +struct A { + constexpr operator bool(this auto&& self) { + return is_lvalue<decltype(self)>; + } +}; + +constexpr A a; +static_assert(static_cast<bool>(a)); +static_assert((bool)a); +static_assert(!static_cast<bool>(A{})); +static_assert(!(bool)A{}); + +struct B : A {}; + +constexpr B b; +static_assert(static_cast<bool>(b)); +static_assert((bool)b); +static_assert(!static_cast<bool>(B{})); +static_assert(!(bool)B{}); + +struct C { + template <typename R, typename T> + explicit constexpr operator R(this T&&) { + return is_lvalue<T>; + } +}; + +constexpr C c; +static_assert(static_cast<bool>(c)); +static_assert((bool)c); +static_assert(!static_cast<bool>(C{})); +static_assert(!(bool)C{}); + +struct D { + explicit constexpr operator bool(this const D&) { return true; } + explicit constexpr operator bool(this const D&&) { return false; } +}; + +constexpr D d; +static_assert(static_cast<bool>(d)); +static_assert((bool)d); +static_assert(!static_cast<bool>(D{})); +static_assert(!(bool)D{});
reply other threads:[~2024-03-06 9:49 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20240306094907.19D8D385843A@sourceware.org \ --to=nshead@gcc.gnu.org \ --cc=gcc-cvs@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: linkBe 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).