admin管理员组

文章数量:1123261

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;
}

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;
}
Share Improve this question asked 9 hours ago JustplayitJustplayit 7171 gold badge7 silver badges23 bronze badges 3
  • Is B always the same type? – Drew Dormann Commented 9 hours ago
  • 2 Are you perhaps looking for something like template<template <typename> class B, typename C, typename... Args> decltype(auto) calledMethod(const Member<B<C>>& method) ? – Igor Tandetnik Commented 9 hours ago
  • I would say minimal reproducible example is needed. Do not describe code, just provide it in minimal complete verifiable version. Something we could just copy paste and try compile/run. – Marek R Commented 9 hours ago
Add a comment  | 

1 Answer 1

Reset to default 0

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

template<typename 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(decval<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>;
    C::Output dummy;
    return dummy;
}

本文标签: cExtracting typename from under typenameStack Overflow