Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the point of a PROTOTYPE macro that merely expands to its arguments?

Tags:

c++

macros

I have a header file which contains

#define PROTOTYPE(s) s 

What is the point of that? Seems like it would just replace the input with itself.

There are TONS of other directives around it, but the only one that appears to have any bearing just checked if it's defined: #ifndef PROTOTYPE. I found some places in HDF4 header files that do this: #define PROTOTYPE. So, none of that really clear up my question. Still seems pretty useless.

Here's how it's used:

CS_RETCODE clientmsg_callback PROTOTYPE(( CS_CONTEXT * context, CS_CONNECTION *connection, CS_CLIENTMSG *clientmsg)); 

This is part of a project which uses Sybase Open Client. clientmsg_callback is later used here:

ct_callback(context, NULL, CS_SET, CS_CLIENTMSG_CB,                   (CS_VOID *)clientmsg_callback); 

I'm going off of a sample program from here:

http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.infocenter.dc35570.1570/html/clcprgde/clcprgde10.htm

clientmsg_callback is implemented later. I think the sample was originally written with C in mind, instead of C++. Perhaps that has something to do with it?

like image 326
Charlie Elverson Avatar asked Sep 03 '19 21:09

Charlie Elverson


People also ask

What is the point of function prototypes?

The function prototypes are used to tell the compiler about the number of arguments and about the required datatypes of a function parameter, it also tells about the return type of the function. By this information, the compiler cross-checks the function signatures before calling it.

What is a macro argument?

Macro Arguments (DEFINE-! ENDDEFINE command) The macro definition can include macro arguments, which can be assigned specific values in the macro call. There are two types of arguments: keyword and positional. Keyword arguments are assigned names in the macro definition; in the macro call, they are identified by name.

What is the purpose of a prototype in C++?

A function prototype is a declaration in C and C++ of a function, its name, parameters and return type before its actual declaration. This enables the compiler to perform more robust type checking.

What is function prototype and what are the benefits of it?

The Function prototype serves the following purposes – 1) It tells the return type of the data that the function will return. 2) It tells the number of arguments passed to the function. 3) It tells the data types of each of the passed arguments.


1 Answers

Back in the olden days of really, really early C, there was no such thing as a prototype. Function argument lists came after the function's parentheses, like this:

square(x) int x; { int y = x * x; return y; } 

These days, of course, the arguments go inside the parentheses:

square(int x) { int y = x * x; return y; } 

Note the "missing" return type; C functions used to implicitly return int, and it was only if you needed a different return type that you had to say what it was.

Function declarations had yet another set of rules. A function declaration in K&R C (the ancient version) had no arguments:

int square(); 

And function prototypes in ANSI C have a list of arguments:

int square(int x); 

During the transition, people used wacky macros so they could compile both ways:

int square(PROTOTYPE(int x)); 

With

#define PROTOTYPE(s) 

that would expand to the first version.

With

#define PROTOTYPE(s) s 

it would expand to the second.

With regard to the "extra" parentheses in the code in the question, they're needed when there is more than one argument in the argument list. Without them, the macro invocation has more than one argument, so won't match a macro defined with just one argument:

PROTOTYPE(int x, int y)   // error: too many arguments PROTOTYPE((int x, int y)) // ok: only one argument (enclosed in parentheses) 
like image 161
Pete Becker Avatar answered Sep 23 '22 18:09

Pete Becker