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);
}
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;
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With