This corrects resolving decltype of a (class) NTTP object as per
[dcl.type.decltype]/1.2 and [temp.param]/6 in the type-dependent case.
Note that in the non-dependent case we resolve the decltype ahead of
time, in which case finish_decltype_type drops the const VIEW_CONVERT_EXPR
wrapper around the TEMPLATE_PARM_INDEX, and the latter has the desired
non-const type.
In the type-dependent case, at instantiation time tsubst drops the
VIEW_CONVERT_EXPR since the substituted NTTP is the already-const object
created by get_template_parm_object. So in this case finish_decltype_type
sees the const object, which this patch now adds special handling for.
PR c++/99631
gcc/cp/ChangeLog:
* semantics.cc (finish_decltype_type): For an NTTP object,
return its type modulo cv-quals.
gcc/testsuite/ChangeLog:
* g++.dg/cpp2a/nontype-class60.C: New test.
(cherry picked from commit
ddd064e3571c4a9e6258c75eba65585a07367712)
case TEMPLATE_PARM_INDEX:
expr = mark_type_use (expr);
type = TREE_TYPE (expr);
+ if (VAR_P (expr) && DECL_NTTP_OBJECT_P (expr))
+ {
+ /* decltype of an NTTP object is the type of the template
+ parameter, which is the object type modulo cv-quals. */
+ int quals = cp_type_quals (type);
+ gcc_checking_assert (quals & TYPE_QUAL_CONST);
+ type = cv_unqualified (type);
+ }
break;
case ERROR_MARK:
--- /dev/null
+// PR c++/99631
+// { dg-do compile { target c++20 } }
+
+struct A { };
+
+template<auto V>
+void f() {
+ static_assert(__is_same(decltype(V), A));
+}
+
+template<class T, T V>
+void g() {
+ static_assert(__is_same(decltype(V), A));
+}
+
+constexpr A a;
+template void f<a>();
+template void g<A, A{}>();