Jonathan O'Connor
Jonathan O'Connor

Reputation: 631

How do I create specializations of alias templates using class template specialization?

I have been playing around with pointers to data members and pointers to member functions, and I am trying to create a template that will create a specialized std::map using the class type and the return type of the function or type of the data member.

So:

template<typename PMember>
using Index = std::map<typename return_type_of<PMember>, typename pod_type_of<PMember>>;

struct Foo { int x; char* get_name(); };
Index<decltype(&Foo::x)> indexOnX; // std::map<int, Foo>
Index<decltype(&Foo::get_name)> indexOnGetName; // std::map<char*, Foo>

I have managed to write 2 alias templates that do that, but sadly they have different names. Code in Godbolt:

include <type_traits>
#include <map>

/**
 * Splits a pointer to member function type into it's class and return type.
 * This only works with member functions that take no arguments.
 */
template <typename MemFunc_t>
struct decompose_member_function_pointer {
   // We hide the implementation within the details struct. These should not be used by users of the class. 
   struct details {
     // This templated function will be used to split M into it's component parts. 
     template <typename C, typename T>
     static T get_returntype(T (C::*v)());

     template <typename C, typename T>
     static C get_classtype(T (C::*v)());
   }; 

   using return_type = decltype(details::get_returntype(std::declval<std::decay_t<MemFunc_t>>()));
   using type = decltype(details::get_classtype(std::declval<std::decay_t<MemFunc_t>>()));
};

template <typename Member_t>
using decompose_member_function_pointer_t = typename decompose_member_function_pointer<Member_t>::type;
template <typename Member_t>
using decompose_member_function_pointer_rt = typename decompose_member_function_pointer<Member_t>::return_type;

template <typename Member_t>
struct decompose_member_object_pointer {
   struct details {
     template <typename C, typename T>
     static T get_returntype(T C::*v);

     template <typename C, typename T>
     static C get_classtype(T C::*v);
   }; 

   using return_type = decltype(details::get_returntype(std::declval<Member_t>()));
   using type = decltype(details::get_classtype(std::declval<Member_t>()));
};

template <typename Member_t>
using decompose_member_object_pointer_t = typename decompose_member_object_pointer<Member_t>::type;
template <typename Member_t>
using decompose_member_object_pointer_rt = typename decompose_member_object_pointer<Member_t>::return_type;


template<typename MemFunc, typename = std::enable_if_t<std::is_member_function_pointer_v<MemFunc>>>
using IndexOnMFP = std::map<
    decompose_member_function_pointer_rt<MemFunc>,
    decompose_member_function_pointer_t<MemFunc>>;

template<typename MemFunc, typename = std::enable_if_t<std::is_member_object_pointer_v<MemFunc>>>
using IndexOnMOP = std::map<
    decompose_member_object_pointer_rt<MemFunc>,
    decompose_member_object_pointer_t<MemFunc>>;


// Now try using these alias templates
struct MyClass
{
    char value;
    int age();
    int age2() const;
};

IndexOnMOP<decltype(&MyClass::value)> indexOnValue;
IndexOnMFP<decltype(&MyClass::age)> indexOnAge;

What I would like is to be able to combine IndexOnMOP and IndexOnMFP into a single template alias, e.g. IndexOnMemberPointer. I appreciate that only function and class templates allow specialization. So far all my attempts have failed.

As a follow up, I would also like to be able to support working out the types from a pointer to a const member function.

Upvotes: 3

Views: 447

Answers (1)

Barry
Barry

Reputation: 303186

I think you may just want:

template <class >
struct trait;

template <class C, class T>
struct trait<T C::*> {
    using class_type = C;
    using ret_type = std::invoke_result_t<T C::*, C>; 
};

template <typename PMember, typename T = trait<PMember>>
using Index = std::map<typename T::ret_type, typename T::class_type>;

This works for both pointers to member functions and pointers to member data. However, you have to be careful with the member data since ret_type here will always be a reference type. So you may want to do something like:

using ret_type = std::conditional_t<
    std::is_function_v<T>,
    std::invoke_result_t<T C::*, C>,
    T>;

Of course, this is C++17, so you can even side-step the decltype:

template <auto PMember, typename T = trait<decltype(PMember)>>
using Index = std::map<typename T::ret_type, typename T::class_type>;

Index<&Foo::x>        indexOnX;       // std::map<int, Foo>
Index<&Foo::get_name> indexOnGetName; // std::map<char*, Foo>

Upvotes: 2

Related Questions