[C + +] Use a function pointer in a macro's arguments

Source: Internet
Author: User

typedef are keywords that are used in C/s + + to create more meaningful aliases for existing types. For normal types, the type is left and the alias is on the right.

typedef int size;

If the definition of a function pointer also conforms to this style, then the following definition is justified:

typedef void (*) (int) Type;

But the compiler is actually using the following definition (the above code does not compile the past):

typedef void (*PTR) (int);

As for the reason, I think (guessing), void (*) (int) is really a function pointer, is a type, but it does not have a name, and the typedef is meant to give the name of the type of the alias, under which the first definition of the way is obviously against. The same is also the renaming of the array (which should have been "typedef char[81] line;" form, but for the same reason it cannot be done):

typedef char LINE[81];

As to whether the compiler in the technical implementation of a more difficult, I do not know.

returning to the topic of this document, I find that this naming of function pointers cannot be used to implement certain macros, such as a macro Ptrtype is also used to rename a type, which is implemented as follows:

#define PTRTYPE (_t, _name) typedef _t _NAME;

But the practical results are:

Ptrtype (void (int), PTR)//compilation not passed

Ptrtype (void (*) (int), PTR)//compilation not passed
Ptrtype (int, Ptr)//This is possible.

typedef void (*PTR) (int);

Ptrtype (PTR, PTR)//this is possible.

So is there a way to make the above macro still effective? With the development of C + +, the concept of templates has been raised, but the problem arises when a function pointer is applied to the concept of a template: if we think that "void (int)" or "Void (*) (int)" Cannot represent itself, The famous function is another way to use it.

Function<int (int) > a;//How to use it now

typdef Int (*ft) (int);//Compiler not supported
Function<ft> A;

In fact, when applied to the template concept, the "FT" is recognized as "void" in the latter way. That's incredible? "FT" is clearly a pointer!

We do not pursue this conceptual difference and why, and how much it is difficult to understand. But this is exactly the way to solve the problem I put forward in this article. We can use this feature of the template to define a new type definition symbol: $.

/**
* $, define type Selector
*/
Template<class _t> struct $
{
/**
* Basic Type
*/
typedef _t TYPE;

/**
* Pointer type
*/
typedef _t *PTR;

/**
* Reference type
*/
typedef _t &Ref;
};

The usage of $ is as follows:

$<void (int);::P tr a = Print;
A (3);
$<int>::type i = 0;
$<int>::ref r = i;

We use this symbol to rewrite the previous ptrtype:

#define PTRTYPE (_t, _name) typedef $<_t;::P tr _name;

This allows the function pointer to be implemented with macro:

Ptrtype (void (int), PTR)
PTR B = Print;
B (5);


[C + +] Use a function pointer in a macro's arguments

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.