Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11/14: How to remove a pointer-to-member from a type?

Tags:

c++

c++11

c++14

In C++11 or C++1y/14: Given a value of pointer-to-member type of the form T C::* I want to get the pointed-to-type T.

For example:

#include <iostream>
using namespace std;

struct T1 { static void f() { cout << "T1" << endl; } };
struct T2 { static void f() { cout << "T2" << endl; } };

struct U1 { T1 x; };
struct U2 { T2 x; };

template<class C> struct V;
template<> struct V<U1> { static constexpr T1 U1::* pm = &U1::x; };
template<> struct V<U2> { static constexpr T2 U2::* pm = &U2::x; };

template<class W>
void f(W pm)
{
     typedef ??? T;
     T::f();
}

int main()
{
        f(V<U1>::pm);
        f(V<U2>::pm);
}

Is there a way to do this? What goes in the ??? above?

Update:

Here is a sanitized version of the libstdc++ implementation of std::remove_pointer:

template<typename T, typename>
struct remove_pointer_helper
{ 
    typedef T type; 
};

template<typename T, typename U>
struct remove_pointer_helper<T, U*>
{ 
    typedef U type; 
};

template<typename T>
struct remove_pointer
    : public remove_pointer_helper<T, typename remove_cv<T>::type>
{};

Update 2:

Here is the final solution, thanks:

#include <iostream>
using namespace std;

template<typename T, typename>
struct remove_member_pointer_helper
{
    typedef T type;
};

template<typename T, typename U, typename C>
struct remove_member_pointer_helper<T, U C::*>
{
    typedef U type;
};

template<typename T>
struct remove_member_pointer
    : public remove_member_pointer_helper<T, typename remove_cv<T>::type>
{};

template<typename T>
using remove_member_pointer_t = typename remove_member_pointer<T>::type;

struct T1 { static void f() { cout << "T1" << endl; } };
struct T2 { static void f() { cout << "T2" << endl; } };

struct U1 { T1 x; };
struct U2 { T2 x; };

template<class C>
struct V;

template<> struct V<U1> { static constexpr T1 U1::* pm = &U1::x; };
template<> struct V<U2> { static constexpr T2 U2::* pm = &U2::x; };

template<class W>
void f(W pm)
{
        remove_member_pointer_t<W>::f();
}

int main()
{
        f(V<U1>::pm);
        f(V<U2>::pm);
}
like image 480
Andrew Tomazos Avatar asked Mar 06 '14 02:03

Andrew Tomazos


1 Answers

This is actually a straightforward exercise in template specialization:

template<class T> struct remove_member_pointer {
    typedef T type;
};
template<class C, class T> struct remove_member_pointer<T C::*> {
    typedef T type;
};
like image 174
Brian Bi Avatar answered Nov 09 '22 22:11

Brian Bi