Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the member function pointer of a destructor?

Assume I have

struct X {
  ~X() {}
};

What's the type of and how do I get the member function pointer of X::~X() in C++03?

I don't want to actually call it, just use in SFINAE to figure if there exists a destructor for a given type.

like image 545
bitmask Avatar asked Jun 02 '12 00:06

bitmask


People also ask

How do you access the members of a pointer?

To access a member function by pointer, we have to declare a pointer to the object and initialize it (by creating the memory at runtime, yes! We can use new keyboard for this). The second step, use arrow operator -> to access the member function using the pointer to the object.

What is pointer to member Declarator in C++?

1) Pointer declarator: the declaration S* D; declares D as a pointer to the type determined by decl-specifier-seq S . 2) Pointer to member declarator: the declaration S C::* D; declares D as a pointer to non-static member of C of type determined by decl-specifier-seq S . nested-name-specifier.

How does a destructor which is a member function?

A destructor is a member function that is invoked automatically when the object goes out of scope or is explicitly destroyed by a call to delete . A destructor has the same name as the class, preceded by a tilde ( ~ ). For example, the destructor for class String is declared: ~String() .

What is the syntax of pointer to member function?

A pointer to a member can be declared and used as shown in the following code fragment: typedef int X::*my_pointer_to_member; typedef void (X::*my_pointer_to_function) (int); int main() { my_pointer_to_member ptiptr = &X::a; my_pointer_to_function ptfptr = &X::f; X xobject; xobject.*ptiptr = 10; cout << "The value of a ...


1 Answers

You can't get the function pointer of a destructor nor a constructor. Nevertheless a destructor always exist for a type, and you can't detect if its private with as access specifiers are not considered by SFINAE.

On the subject of invoking what would be the destructor of a scalar type, the standard says [class.dtor]/16:

[Note:the notation for explicit call of a destructor can be used for any scalar type name (5.2.4). Allowing this makes it possible to write code without having to know if a destructor exists for a given type. For example,

typedef int I;

I* p;

p->I::~I();

—end note]

like image 51
K-ballo Avatar answered Sep 19 '22 05:09

K-ballo