c++templatestypes

Extracting typename from under typename


I have the following method in a class A in C++ 11 where I know that TMethod gets converted to a typename B<C>.
How can I extract the C typename so that I can access further members from it like in the example below?

template<typename TMethod, typename... Args>
decltype(auto) calledMethod(const Member<TMethod>& method)
{ 
    // somehow extract C from under TMethod
    C::Output dummy;
    return dummy;
}

Solution

  • You can use a little helper like get_T in the following code that makes it possible to retrieve X from T<X> through class template specialization.

    ///////////////////////////////////////////////////////////////
    template <typename T> 
    struct get_T;
    
    template <template <typename> class X,typename T>
    struct get_T<X<T>> { using type = T; };
    
    ///////////////////////////////////////////////////////////////
    template<typename T> struct Member {};
    
    struct TMethod { struct Output{}; };
    
    template<typename TMethod, typename... Args>
    get_T<TMethod> calledMethod(const Member<TMethod>& method)
    {
        // somehow extract C from under TMethod
        using C = get_T<TMethod>;
         
        typename C::Output dummy;
        return dummy;
    }
    
    int main()  {}
    

    DEMO