C++ C++;11/14:如何从类型中删除指向成员的指针?

C++ C++;11/14:如何从类型中删除指向成员的指针?,c++,c++11,c++14,C++,C++11,C++14,在C++11或C++1y/14中:给定一个指向成员类型的指针值,其形式为tc:*我想得到指向类型T的指针 例如: #include <iostream> using namespace std; struct T1 { static void f() { cout << "T1" << endl; } }; struct T2 { static void f() { cout << "T2" << endl; } }; struct

在C++11或C++1y/14中:给定一个指向成员类型的指针值,其形式为
tc:*
我想得到指向类型T的指针

例如:

#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);
}
更新2:

以下是最终解决方案,谢谢:

#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);
}
#包括
使用名称空间std;
模板
结构删除\成员\指针\辅助对象
{
T型;
};
模板
结构删除\成员\指针\辅助对象
{
U型;
};
模板
结构删除\成员\指针
:public remove\u member\u pointer\u helper
{};
模板
使用remove\u member\u pointer\u t=typename remove\u member\u pointer::type;

struct T1{static void f(){cout这实际上是模板专门化中一个简单的练习:

template<class T> struct remove_member_pointer {
    typedef T type;
};
template<class C, class T> struct remove_member_pointer<T C::*> {
    typedef T type;
};
模板结构删除\u成员\u指针{
T型;
};
模板结构删除\u成员\u指针{
T型;
};

您可以创建一个特性,专门用于指向成员的指针,并公开与类类型相同的类型。@0x499602D2我不清楚OP到底想做什么。无论如何,希望这个答案足够说明问题,使其适合自己的目的。您可以提到使用remove\u member\u pointer\t的
模板=typename remove_member_pointer::type;
否则这是“仅仅”C++03.@Potatoswatter:
std::remove_member_pointer
在类型特征中不存在,对吗?也许应该存在。@AndrewTomazos认为没有太多的调用。另外,作为一个通用函数,它应该如何处理PTMFs?@Potatoswatter:我的需求来自反射。关于PTMFs的好问题,需要看看这个。
template<class T> struct remove_member_pointer {
    typedef T type;
};
template<class C, class T> struct remove_member_pointer<T C::*> {
    typedef T type;
};