Replacing text macros

The preprocessor supports text macro replacement. Function-like text macro replacement is also supported.

# Notes

The function-local predefined variable func is not a predefined macro, but it is usually used together with FILE and LINE, e.g. by assert.

# Example

#include <iostream>
 
// Make function factory and use it
#define FUNCTION(name, a) int fun_##name() { return a; }
 
FUNCTION(abcd, 12)
FUNCTION(fff, 2)
FUNCTION(qqq, 23)
 
#undef FUNCTION
#define FUNCTION 34
#define OUTPUT(a) std::cout << "output: " #a << '\n'
 
// Using a macro in the definition of a later macro
#define WORD "Hello "
#define OUTER(...) WORD #__VA_ARGS__
 
int main()
{
    std::cout << "abcd: " << fun_abcd() << '\n';
    std::cout << "fff: " << fun_fff() << '\n';
    std::cout << "qqq: " << fun_qqq() << '\n';
 
    std::cout << FUNCTION << '\n';
    OUTPUT(million); //note the lack of quotes
 
    std::cout << OUTER(World) << '\n';
    std::cout << OUTER(WORD World) << '\n';
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
CWG 2908C++98it was unclear whether LINE expands to the currentphysical line number or the current logical line numberexpands to the currentphysical line number
LWG 294C++98a translation unit that includes a standard library header could containmacros that define names declared in other standard library headersprohibited
P2621R2C++23universal character names were not allowedto be formed by token concatenationallowed

# See also