CX Framework
Cross-platform C utility framework
Loading...
Searching...
No Matches
Macros

Macros

#define count_macro_args(...)
 
#define opt_arg(...)
 
#define tokconcat(x, y)   _tokconcat_actual(x, y)
 
#define tokcat2(x, y)   _tokconcat_actual(x, y)
 Concatenate two tokens (alias for tokconcat)
 
#define tokcat3(x1, x2, x3)   tokcat2(tokcat2(x1, x2), x3)
 Concatenate three tokens.
 
#define tokcat4(x1, x2, x3, x4)   tokcat2(tokcat3(x1, x2, x3), x4)
 Concatenate four tokens.
 
#define tokcat5(x1, x2, x3, x4, x5)   tokcat2(tokcat4(x1, x2, x3, x4), x5)
 Concatenate five tokens.
 
#define tokconcatU(x, y)   _tokconcatU_actual(x, y)
 
#define tokcatU2(x, y)   _tokconcatU_actual(x, y)
 Concatenate two tokens with underscore (alias for tokconcatU)
 
#define tokcatU3(x1, x2, x3)   tokcatU2(tokcatU2(x1, x2), x3)
 Concatenate three tokens with underscores.
 
#define tokcatU4(x1, x2, x3, x4)   tokcatU2(tokcatU3(x1, x2, x3), x4)
 Concatenate four tokens with underscores.
 
#define tokcatU5(x1, x2, x3, x4, x5)   tokcatU2(tokcatU4(x1, x2, x3, x4), x5)
 Concatenate five tokens with underscores.
 
#define tokstring(x)   _tokstring_actual(x)
 
#define tokeval(...)   __VA_ARGS__
 
#define unused_noeval(x)   (true ? (void)0 : (void)(x))
 
#define nop_stmt   ((void)0)
 

Detailed Description

Commonly-used utility macros

Macro Definition Documentation

◆ count_macro_args

#define count_macro_args (   ...)

Count the number of macro arguments (up to 50)

Returns the number of arguments passed to the macro. This is commonly used to implement variadic macros that need to know how many arguments were provided. When zero arguments are passed, returns 1 (a limitation of the preprocessor).

This implementation uses an extra indirection macro to work around MSVC preprocessor issues.

Example:

count_macro_args(a, b, c) // Expands to 3
count_macro_args(x) // Expands to 1
count_macro_args() // Expands to 1 (not 0)
#define count_macro_args(...)
Definition args.h:81

Definition at line 81 of file args.h.

◆ nop_stmt

#define nop_stmt   ((void)0)

No-operation statement

A statement that does nothing, useful for satisfying syntax requirements or as a placeholder. Commonly used in macro definitions where a statement is required but no action is needed.

Example:

#define DEBUG_LOG(msg) nop_stmt // Debug logging disabled

Definition at line 44 of file unused.h.

◆ opt_arg

#define opt_arg (   ...)
Value:
_opt_arg_dispatch_1(count_macro_args(dummy __VA_OPT__(, ) __VA_ARGS__), \
(dummy __VA_OPT__(, ) __VA_ARGS__))

Extract a single optional argument, defaulting to 0 if not present

This macro is used to implement functions with optional arguments by providing a default value of 0 when the argument is omitted. It's commonly used for optional flags parameters in function wrappers.

The implementation differs between compilers due to preprocessor differences:

  • Standard compilers use __VA_OPT__ for clean optional argument handling
  • MSVC uses a workaround for its non-conformant preprocessor

Example:

// In a macro wrapper:
#define myFunc(required, ...) _myFunc(required, opt_arg(__VA_ARGS__))
myFunc(x); // Expands to: _myFunc(x, 0)
myFunc(x, 5); // Expands to: _myFunc(x, 5)

Definition at line 31 of file optarg.h.

◆ tokconcat

#define tokconcat (   x,
 
)    _tokconcat_actual(x, y)

Concatenate two tokens after macro expansion

Uses an extra level of indirection to ensure both tokens are expanded before concatenation. Without this indirection, the ## operator would paste tokens before they are expanded.

Example:

#define PREFIX foo
tokconcat(PREFIX, _bar) // Expands to: foo_bar
#define tokconcat(x, y)
Definition tokens.h:20

Definition at line 20 of file tokens.h.

◆ tokconcatU

#define tokconcatU (   x,
 
)    _tokconcatU_actual(x, y)

Concatenate two tokens with underscore separator after macro expansion

Similar to tokconcat but inserts an underscore between the tokens.

Example:

#define PREFIX foo
tokconcatU(PREFIX, bar) // Expands to: foo_bar
#define tokconcatU(x, y)
Definition tokens.h:44

Definition at line 44 of file tokens.h.

◆ tokeval

#define tokeval (   ...)    __VA_ARGS__

Force macro expansion of arguments

This utility macro forces the preprocessor to fully expand its arguments. It's primarily used to work around MSVC preprocessor quirks and to separate tokens in macro processing.

Example:

#define PAIR (a, b)
tokeval PAIR // Expands to: a, b
#define tokeval(...)
Definition tokens.h:84

Definition at line 84 of file tokens.h.

◆ tokstring

#define tokstring (   x)    _tokstring_actual(x)

Convert a token to a string literal after macro expansion

Uses an extra level of indirection to ensure the token is expanded before stringification. The # operator would stringify the token name without expansion otherwise.

Example:

#define VALUE 123
tokstring(VALUE) // Expands to: "123"
#define tokstring(x)
Definition tokens.h:70

Definition at line 70 of file tokens.h.

◆ unused_noeval

#define unused_noeval (   x)    (true ? (void)0 : (void)(x))

Mark a variable as intentionally unused without evaluating it

This macro silences "unused variable" warnings without actually evaluating the expression. The expression is used in an unevaluated context (the false branch of a ternary operator) so it has no runtime cost and doesn't trigger side effects.

The GCC implementation uses a helper function to work around compiler-specific issues.

Example:

int result = someFunction();
unused_noeval(result); // Silence warning without evaluating
#define unused_noeval(x)
Definition unused.h:31

Definition at line 31 of file unused.h.