C++ Logo

std-proposals

Advanced search

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

From: Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]>
Date: Fri, 28 Apr 2023 10:55:33 -0400
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.

–Arthur

Received on 2023-04-28 14:55:46