liveBook · Manning

Function-like macro

A function-like macro is a macro invoked with function-like syntax that accepts token input and expands or transforms it into generated source.

How 3 Manning books explain it

Modern C, Third Edition

A function-like macro is a C preprocessor macro invoked with parentheses and arguments that expands its invocation into replacement source text.

How it is used
Uses function-like macros for library-style interfaces, compile-time checks, tracing, and interfaces that ordinary functions cannot express as directly.
How deep it goes
Explains textual replacement, evaluation and syntax hazards, argument checking, and invocation context, with an inline-function alternative.
Examples
Uses putchar replacement, a square macro with a side-effecting argument, a statement wrapper, and string-literal checking.
What you can do after
Evaluate function-like macros for repeated evaluation and syntactic hazards, and choose an inline function when appropriate.

Read how Modern C, Third Edition explains function-like macro →

Modern C

A function-like macro is a C macro defined with parameters that expands preprocessing tokens when invoked with an argument list resembling a function call.

How it is used
Uses macros to complement C interfaces when ordinary functions cannot express the needed behavior.
How deep it goes
Explains argument parsing, substitution and rescanning, then applies macro design rules to side effects and argument checking.
Examples
Works through putchar, a precedence-sensitive sum, a repeated-evaluation maximum and a string-literal check.
What you can do after
Apply it to design macro interfaces, identify expansion hazards and choose an inline function when appropriate.

Read how Modern C explains function-like macro →

Write Powerful Rust Macros

A function-like macro is a Rust procedural macro invoked with the ! operator that accepts arbitrary token input and replaces that input with generated output.

How it is used
Generates replacement Rust from flexible token input for transformations and mini-DSLs beyond annotated items.
How deep it goes
Works through macro signatures, token parsing, custom parsers, quoted output, block scoping, and compiler-guided debugging.
Examples
Uses a struct-accessor macro, dot-separated function composition, YAML configuration generation, and implementation exercises.
What you can do after
Build it from scratch by parsing token input, generating replacement code, and selecting a narrower macro when possible.

Read how Write Powerful Rust Macros explains function-like macro →

What to know first

Related concepts

Questions about function-like macro

What is function-like macro?

A function-like macro is a macro invoked with function-like syntax that accepts token input and expands or transforms it into generated source.

Which Manning books explain function-like macro?

Modern C, Third Edition, Modern C and Write Powerful Rust Macros.

What should I know before learning function-like macro?

Manning books that teach function-like macro usually introduce function, macro, comma operator and _Generic first.

What is the difference between function-like macro and function?

A function-like macro is a macro invoked with function-like syntax that accepts token input and expands or transforms it into generated source. A function is a callable unit of program code that performs a computation or operation, can accept inputs, and may produce a result or side effect.

What is function-like macro a kind of?

Function-like macro is a kind of procedural macro. A procedural macro is Rust code that transforms an input token stream into generated code during compilation.

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.