c++functiontypesconstantsmember

C++ Get type of a member function (return type and signature) without the 'const' qualifier of the member function


Is it possible to get the type of a member function (return type and signature) without the 'const' qualifier of the member function?

I tried it so far with decltype(T) and std::remove_const / std::decay.

Example:

void Func(std::string, int) const -> void(std::string, int)

Edit for clarification:

What I have:

void Func(std::string, int) const

What I want:

void(std::string, int)

Solution

  • Here is a way of doing this using CTAD:

    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