C++ Logo

std-proposals

Advanced search

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

From: Edward Catmur <ecatmur_at_[hidden]>
Date: Fri, 28 Apr 2023 09:47:59 -0500
On Fri, 28 Apr 2023 at 06:36, 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.
>
> The following is the first draft(crude) example of what I am suggesting.
> Also this topic of getting back the type of a non-static member function
> seems to be a fairly common question on sites like stackoverflow and
> looking at those question I notice that almost all of them tries to use *decltype
> *first to solve this problem but then came to realise that, using *decltype
> *for like this is not allowed. So this trait *might provide a standard
> way* of getting back the "ordinary function type" associated with the
> non-static member function.
>
> struct C{ void Func(std::string, int) const; void Bar(int);};template<typename T, typename Ret, typename... Args>struct GetType{ using type = Ret(Args...); GetType(Ret (T::*)(Args...) const) { } GetType(Ret (T::*)(Args...) ) { }};int main(){ using ret1 = decltype(GetType(&C::Func))::type; // void(std::string, int) as expected using ret2 = decltype(GetType(&C::Bar))::type; //void(int) as expected }
>
> [Demo](https://godbolt.org/z/rd49WW8ef)
>
>
That's doing 2 things at once: removing the member pointer qualification,
and removing const. It's better to just perform the former transformation,
since the user can compose with std::remove_const if they choose:

template<class> struct remove_member_pointer;
template<class C, class M> struct remove_member_pointer<M C::*> :
std::type_identity<M> {};



> ----------------------------------------
>
> Note that I usually use CTAD for this kind of problem as it reduces
> writing some extra code. Also this is just a demo to show the overall idea
> of what I am proposing. I realise that in order for this to work with
> potentially all non-static member functions(with any type), we might have
> to modify it as needed.
>
> Regards,
> Anoop Rana
> M.Sc Applied Physics
>
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2023-04-28 14:48:12