Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
}
like image 446
Justplayit Avatar asked Oct 20 '25 23:10

Justplayit


1 Answers

I like using what I call a meta function for this. Having

template<template <typename> class T, typename C>
auto get_template_type(T<C>) -> C;

we can write an alias to get C like

template<typename T>
using template_type_t = decltype(get_template_type(declval<T>()));

and then in your function you you can get C like

template<typename TMethod, typename... Args>
decltype(auto) calledMethod(const Member<TMethod>& method)
{ 
    using C = template_type_t<TMethod>;
    typename C::Output dummy;
    return dummy;
}
like image 80
NathanOliver Avatar answered Oct 23 '25 13:10

NathanOliver