已删除指向函数成员的指针的类型

时间:2013-01-25 15:06:56

标签: c++ c++11 function-pointers

类型:

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;
}

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

#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)之类的内容会引发编译错误(无效类型)。

2 个答案:

答案 0 :(得分:3)

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

remove_pointer不会更改类型。

答案 1 :(得分: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::*转到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;
};