Skip to content

Commit 63a6c87

Browse files
committed
Make std::meta::type_of of a nonstatic member function a plain function type []
For a reflection of a nonstatic member function, type_of returned the routine type of the function, whose this_class is set. That type is not identical to the corresponding function type, so type_of(^^S::f) did not compare equal to ^^int(int) and the spliced type did not match a partial specialization such as X<R(A...)>. The type of a nonstatic member function is a plain function type ([dcl.fct]), including its cv-qualifiers and ref-qualifier. type_of now drops the this_class with routine_type_without_this_class (which keeps the qualifiers), building the new type in file scope memory so that the reflection outlives the evaluation. Also: a new test (reflections/type_of_member_function.sft.cpp) and a src/Changes entry.
1 parent 9bda256 commit 63a6c87

5 files changed

Lines changed: 100 additions & 1 deletion

File tree

‎src/Changes‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
Version 7.1
22

3+
10/8/26
4+
std::meta::type_of for nonstatic member functions
5+
6+
For a reflection of a nonstatic member function, std::meta::type_of returned
7+
a type that still carried the class of the function, so it did not compare
8+
equal to the reflection of the corresponding function type, and the spliced
9+
type did not match a partial specialization for function types. The type of
10+
a nonstatic member function is a plain function type ([dcl.fct]), including
11+
its cv-qualifiers and ref-qualifier; type_of now returns that type. For
12+
example:
13+
14+
#include <experimental/meta>
15+
struct M {
16+
int f(int, double);
17+
void g() const noexcept;
18+
};
19+
static_assert(type_of(^^M::f) == ^^int(int, double));
20+
// Previously failed.
21+
static_assert(type_of(^^M::g) == ^^void() const noexcept);
22+
// Previously failed.
23+
24+
325
10/8/26
426
std::meta::parent_of for namespaces
527

‎src/interpret.c‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15743,7 +15743,10 @@ static a_boolean do_constexpr_std_meta_type_of(
1574315743
a_byte *result_storage,
1574415744
a_byte *complete_obj)
1574515745
/*
15746-
Implement std::meta::type_of(info).
15746+
Implement std::meta::type_of(info). The type of a nonstatic member function
15747+
is a plain function type ([dcl.fct]): the class of the implicit object
15748+
parameter is not part of it, but its cv-qualifiers and ref-qualifier are
15749+
(e.g., "void() const" for "void f() const").
1574715750

1574815751
See do_constexpr_intrinsic_call for the meaning of the parameters.
1574915752
*/
@@ -15788,6 +15791,15 @@ See do_constexpr_intrinsic_call for the meaning of the parameters.
1578815791
default:
1578915792
break;
1579015793
} /* switch */
15794+
if (tp != NULL && tp->kind == tk_routine &&
15795+
tp->variant.routine.extra_info->this_class != NULL) {
15796+
/* Drop the class of a nonstatic member function from its type. The new
15797+
type must outlive the evaluation. */
15798+
a_memory_region_number region_to_switch_back_to;
15799+
switch_to_file_scope_region(&region_to_switch_back_to);
15800+
tp = routine_type_without_this_class(tp, /*copy_default_args=*/FALSE);
15801+
switch_back_to_original_region(region_to_switch_back_to);
15802+
} /* if */
1579115803
if (tp == NULL) {
1579215804
info_with_pos(ec_invalid_reflection_for_intrinsic,
1579315805
&call_node->position, ips);

‎tests/expectations/edg_x86_64_cp/reflections.log‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ members_of_order.sft.cpp:fp:-DTEST_NUMBER=1:REQUIREMENTS NOT MET:
33
parent_of_namespace.sft.cpp:fp:-DTEST_NUMBER=1:REQUIREMENTS NOT MET:
44
source_location_of.sft.cpp:fp:-DTEST_NUMBER=1:REQUIREMENTS NOT MET:
55
splice_protected_access.sft.cpp:rp:-DTEST_NUMBER=1:FAIL(COMPILE):
6+
type_of_member_function.sft.cpp:fp:-DTEST_NUMBER=1:REQUIREMENTS NOT MET:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fe_only -DTEST_NUMBER=1 --c++26 --set_flag=reflection Test_name.c
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//type:fp
2+
//use_system_includes: true
3+
//edg_header_pack: exp_meta
4+
//require:DO_IL_LOWERING 1
5+
6+
/*
7+
std::meta::type_of of a nonstatic member function is a plain function type
8+
([dcl.fct]): the class is not part of it, but the cv-qualifiers, the
9+
ref-qualifier, and the exception specification are. So it compares equal to
10+
the reflection of that function type, and the spliced type matches partial
11+
specializations for function types.
12+
*/
13+
14+
#include <experimental/meta>
15+
16+
using namespace std::meta;
17+
18+
struct M {
19+
int f(int, double);
20+
void g() const noexcept;
21+
void h() volatile &&;
22+
static int s(int);
23+
void d(int = 1);
24+
void e(this M &, int);
25+
virtual void v();
26+
};
27+
28+
static_assert(type_of(^^M::f) == ^^int(int, double));
29+
static_assert(type_of(^^M::g) == ^^void() const noexcept);
30+
static_assert(type_of(^^M::h) == ^^void() volatile &&);
31+
static_assert(type_of(^^M::s) == ^^int(int));
32+
static_assert(type_of(^^M::d) == ^^void(int));
33+
static_assert(type_of(^^M::e) == ^^void(M &, int));
34+
static_assert(type_of(^^M::v) == ^^void());
35+
static_assert(is_function_type(type_of(^^M::f)));
36+
37+
template <typename> struct Kind { static constexpr int value = 0; };
38+
template <typename R, typename... A> struct Kind<R(A...)> {
39+
static constexpr int value = 1;
40+
};
41+
template <typename R, typename... A> struct Kind<R(A...) const noexcept> {
42+
static constexpr int value = 2;
43+
};
44+
template <typename R, typename... A> struct Kind<R(A...) volatile &&> {
45+
static constexpr int value = 3;
46+
};
47+
48+
static_assert(Kind<typename[:type_of(^^M::f):]>::value == 1);
49+
static_assert(Kind<typename[:type_of(^^M::g):]>::value == 2);
50+
static_assert(Kind<typename[:type_of(^^M::h):]>::value == 3);
51+
52+
// The function type gives back the pointer-to-member type.
53+
using F = [:type_of(^^M::f):];
54+
using G = [:type_of(^^M::g):];
55+
using PF = F M::*;
56+
using PG = G M::*;
57+
static_assert(dealias(^^PF) == ^^int (M::*)(int, double));
58+
static_assert(dealias(^^PG) == ^^void (M::*)() const noexcept);
59+
60+
// The result does not depend on where type_of is evaluated.
61+
consteval info type_of_g() { return type_of(^^M::g); }
62+
constexpr info g_type = type_of_g();
63+
static_assert(g_type == type_of(^^M::g));

0 commit comments

Comments
 (0)