From: Patrick Palka Date: Wed, 17 Jan 2024 18:01:01 +0000 (-0500) Subject: c++: address of class NTTP object as targ [PR113242] X-Git-Tag: basepoints/gcc-15~1950 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=68cea2d32a9fd525154b6a48042e5835d4c5e371;p=thirdparty%2Fgcc.git c++: address of class NTTP object as targ [PR113242] invalid_tparm_referent_p was rejecting using the address of a class NTTP object as a template argument, but this should be fine. PR c++/113242 PR c++/99493 gcc/cp/ChangeLog: * pt.cc (invalid_tparm_referent_p) : Suppress DECL_ARTIFICIAL rejection test for class NTTP objects. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/nontype-class61.C: New test. * g++.dg/cpp2a/nontype-class62.C: New test. Reviewed-by: Jason Merrill --- diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index b6117231de1c..f82d018c9816 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -7217,8 +7217,10 @@ invalid_tparm_referent_p (tree type, tree expr, tsubst_flags_t complain) * a string literal (5.13.5), * the result of a typeid expression (8.2.8), or * a predefined __func__ variable (11.4.1). */ - else if (VAR_P (decl) && DECL_ARTIFICIAL (decl)) + else if (VAR_P (decl) && DECL_ARTIFICIAL (decl) + && !DECL_NTTP_OBJECT_P (decl)) { + gcc_checking_assert (DECL_TINFO_P (decl) || DECL_FNAME_P (decl)); if (complain & tf_error) error ("the address of %qD is not a valid template argument", decl); diff --git a/gcc/testsuite/g++.dg/cpp2a/nontype-class61.C b/gcc/testsuite/g++.dg/cpp2a/nontype-class61.C new file mode 100644 index 000000000000..4033cf0f271a --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/nontype-class61.C @@ -0,0 +1,25 @@ +// PR c++/113242 +// { dg-do compile { target c++20 } } + +struct wrapper { int n; }; + +template +void f1() { + static_assert(X.n == 42); +} + +template +void f2() { + static_assert(X->n == 42); +} + +template +void g() { + f1(); + f2<&X>(); +} + +int main() { + constexpr wrapper X = {42}; + g(); +} diff --git a/gcc/testsuite/g++.dg/cpp2a/nontype-class62.C b/gcc/testsuite/g++.dg/cpp2a/nontype-class62.C new file mode 100644 index 000000000000..f5068fbd0099 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/nontype-class62.C @@ -0,0 +1,8 @@ +// PR c++/99493 +// { dg-do compile { target c++20 } } + +struct owner{int m;}; +struct view{const int*m;constexpr view(const owner&o):m{&o.m}{}}; +templatestruct constant{}; +templateconstexpr constantv{}; +constexpr auto a=v;