C++ Logo

std-proposals

Advanced search

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

From: Anoop Rana <ranaanoop986_at_[hidden]>
Date: Fri, 28 Apr 2023 22:47:32 +0530
@Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]>


> `C::Func` isn't a `void(std::string, int)` at all. static_assert
> (std::is_same_v<decltype(&S::f), int (S::*)() const>);
>

No, you're actually/technically comparing pointers(though member function
pointers are distinct from ordinary pointers as per the standard) and not
the actual function types. The standard doesn't allow decltype(S::f). Note
the missing *&* operator in decltype(S::f) . That is, ` S::f` and `&S::f`
are different *entities*.

--------------------------------------------------------------------------------

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.
>

No, there is no such thing as a *member function type *in the C++ standard.
The standard specifically says: " There are no special member function types
or data member types. The type of a non-static member function is an
ordinary function type." So your statement that "non-static member function
is not an ordinary function type in the usual sense of the phrase, it's a
member function type" is completely wrong.

--------------------------------------------------------------------------------

It's a function of three parameters: the string, the int, and the hidden
> "this" parameter
>

No, there is no such thing as hidden "this" parameter(implicit "this"
parameter) in the C++ standard(at least not upto C++20 afaik). What is
there is the "implicit object parameter" and that too is used only during
overload resolution. There is cwg2660
<https://cplusplus.github.io/CWG/issues/2660.html> discussing this.


On Fri, 28 Apr 2023 at 22:03, Jason McKesson via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> 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?
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2023-04-28 17:17:45