Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't putc() be implemented as a macro in C++? (Or can it?)

Reading en.cppreference.com on fputc()/putc(), I stumbled upon the following statement:

In C, putc() may be implemented as a macro, which is disallowed in C++.

Is this true? If so, where (in the C++ standard) is this stated?

Possibly related: https://stackoverflow.com/questions/10712423

like image 869
plexando Avatar asked Mar 01 '23 18:03

plexando


1 Answers

putc is specified as a function in the C standard:

7.19.7.8 The putc function

The C standard permits any C standard library function to be implemented as a macro (a real function implementation still needs to be provided, but if a macro is defined in the header, that's what will be used):

7.1.4 Use of library functions

Any function declared in a header may be additionally implemented as a function-like macro

But the C++ standard specifically states that these must be defined as functions:

17.4.1.2.6 Headers

Names that are defined as functions in C shall be defined as functions in the C++ Standard Library.

The footnote explicitly clarifies that an additional macro definition is not allowed:

This disallows the practice, allowed in C, of providing a "masking macro" in addition to the function prototype. The only way to achieve equivalent "inline" behavior in C++ is to provide a definition as an extern inlin

like image 63
Mikel Rychliski Avatar answered Mar 15 '23 22:03

Mikel Rychliski