Search code examples
c++templatesc++14template-argument-deduction

Determining the type of the template parameter method argument


There are many IEnumXXXX type COM interfaces that have a pure virtual method Next, like this:

IEnumString : IUnknown {
...
virtual HRESULT Next(ULONG, LPOLESTR*, ULONG*) = 0;
...
};

IEnumGUID : IUnknown {
...
virtual HRESULT Next(ULONG, GUID*, ULONG*) = 0;
...
};

Need a template, like this:

enum_value_type<IEnumString>::type // LPOLESTR
enum_value_type<IEnumGUID>::type   // GUID

Solution

  • Something along these lines:

    template <typename T> struct ExtractArgType;
    
    template <typename C, typename T>
    struct ExtractArgType<HRESULT (C::*)(ULONG, T*, ULONG*)>{
        using type = T;
    };
    
    template <typename IEnum>
    struct enum_value_type {
        using type = typename ExtractArgType<decltype(&IEnum::Next)>::type;
    };
    

    Demo