C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Add trait to get the type of a member function

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Fri, 28 Apr 2023 12:33:34 -0400
On Fri, Apr 28, 2023 at 10:55 AM Arthur O'Dwyer via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> On Fri, Apr 28, 2023 at 7:36 AM Anoop Rana via Std-Proposals <std-proposals_at_[hidden]> wrote:
>>
>> Since the type of a non-static member function is an ordinary function type and since we can't use decltype on a non-static member function to get its type, I propose that we should add a trait to get back the type of a non-static member function.
>
>
> I don't understand what you mean. The type of a non-static member function is not an "ordinary function type" in the usual sense of the phrase; it's a member function type.
> And `decltype` works just fine:
>
> // https://godbolt.org/z/Ea388neob
> struct S {
> int f() const;
> };
> static_assert(std::is_same_v<decltype(&S::f), int (S::*)() const>);
>
> Is it possible that you were actually trying to use `decltype(&S::g)` on an overload set?
> https://godbolt.org/z/fj94db8cT
>
> [...]
>>
>> using ret1 = decltype(GetType(&C::Func))::type; // void(std::string, int) as expected
>
>
> But that's not what's expected! `C::Func` isn't a `void(std::string, int)` at all. It's a function of three parameters: the string, the int, and the hidden "this" parameter of type `C`. I cannot just pass a string and an int to it and have it work. It needs all three parameters in order to do its job.

You may have missed the `GetType` metafunction there.

A deeper question is this: what exactly do you plan to *do* with this
information? Is the goal to be able to iterate through the parameters
to the member function with the same metaprogramming tools you would
use on non-member functions?

Received on 2023-04-28 16:33:48