1

类型:

std::remove_pointer<int(*)(int)>::type

int(int)。这段代码:

#include <iostream>
#include <type_traits>

using namespace std;

int main()
{
   cout << boolalpha;
   cout << is_same<remove_pointer<int(*)(int)>, int(int)>::value;
   cout << endl;
}

打印“真实”。但是,“函数成员”的(书面)类型是什么?

#include <iostream>
#include <type_trais>

using namespace std;

struct A {};

int main()
{
    cout << boolalpha;
    cout << is_same<remove_pointer<int(A::*)(int)>, int(int)>::value;
    cout << endl;
}

返回false。诸如int A::(int)抛出编译错误(无效类型)之类的东西。

4

2 回答 2

3

指向成员的指针不是指针。

remove_pointer不会改变类型。

于 2013-01-25T15:10:37.493 回答
3

它是这样的:非成员类型是对象或函数类型:

T = int;                 // object type
T = double(char, bool)   // function type

对于非静态类成员,您只能有指向成员类型的指针。这些是以下形式:

class Foo;

PM = U Foo::*;           // pointer to member type

问题是什么U

U = int                 =>  PM = int Foo::*                  // pointer-to-member-object
U = double(char, bool)  =>  PM = double (Foo::*)(char, bool) // pointer-to-member-function

“指向成员的指针”不是指针,因此您不能从中“删除指针”。充其量你可以得到底层类型,即从PM = U Foo::*to U。据我所知,不存在这样的特征,但很容易编造出来:

template <typename> struct remove_member_pointer;

template <typename U, typename F> struct remove_member_pointer<U F::*>
{
    typedef U member_type; 
    typedef F class_type;
};
于 2013-01-25T15:16:10.830 回答