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

Viewed 117
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

Related