C++ Utilities
Loading...
Searching...
No Matches
macros.h

Several useful and generally well known macros.
See the Macros page for all the details. More...

Go to the source code of this file.

Macros

#define STRINGISE(s)
 STRINGISE invokes the pre-processor stringising operator, fully expanding any macro argument first!
#define CONCAT(a, b)
 CONCAT concatenates two symbols making sure to fully expand those symbols if they happen to be macros themselves.
#define VERSION_STRING(...)
 VERSION_STRING creates a semantic version string from 1, 2, or 3 arguments.
#define RUN(...)
 RUN prints the line of code to the console, executes it, then optionally outputs one or two results.
#define OVERLOAD(macro, ...)
 OVERLOAD makes it appear that a macro can be overloaded by the number of passed arguments.
#define COMPILER_NAME   "Unidentified Compiler"
 COMPILER_NAME is a macro that expands to the current compiler name & version as a string.

Detailed Description

Several useful and generally well known macros.
See the Macros page for all the details.

Macro Definition Documentation

◆ COMPILER_NAME

#define COMPILER_NAME   "Unidentified Compiler"

COMPILER_NAME is a macro that expands to the current compiler name & version as a string.

Sometimes useful to annotate test or benchmark output etc.

Note: We can add more compilers if needed.

◆ CONCAT

#define CONCAT ( a,
b )
Value:
CONCAT_IMPL(a, b)

CONCAT concatenates two symbols making sure to fully expand those symbols if they happen to be macros themselves.

Example

#define FOO2 foo
#define BAR2 FOO2
int foofoo = 42;
auto value = CONCAT(FOO2, BAR2);
assert(value == foofoo);
#define assert(cond,...)
An assertion macro that checks a boolean condition cond. On failure, the assertion prints an error ...
Definition assert.h:99
#define CONCAT(a, b)
CONCAT concatenates two symbols making sure to fully expand those symbols if they happen to be macros...
Definition macros.h:73

◆ OVERLOAD

#define OVERLOAD ( macro,
... )
Value:
CONCAT(macro, ARG_COUNT(__VA_ARGS__))(__VA_ARGS__)

OVERLOAD makes it appear that a macro can be overloaded by the number of passed arguments.

#define FOO(...) OVERLOAD(FOO, __VA_ARGS__) will make 'FOO' appear overloaded on the number of passed args:

  • FOO() calls the zero arg version which must be called FOO0
  • FOO(a) calls the one arg version FOO1(a), which must be called FOO1.
  • FOO(a,b) calls the two arg version FOO2(a, b). That version must be called FOO2.
  • etc. etc.

You supply whichever specific FOO0(), FOO1(a), FOO2(a,b), FOO2(a,b,c), implementations that make sense, but the macro consumer can just call FOO(...) and automatically get the correct one.

◆ RUN

#define RUN ( ...)
Value:
OVERLOAD(RUN, __VA_ARGS__)
#define RUN(...)
RUN prints the line of code to the console, executes it, then optionally outputs one or two results.
Definition macros.h:108
#define OVERLOAD(macro,...)
OVERLOAD makes it appear that a macro can be overloaded by the number of passed arguments.
Definition macros.h:134

RUN prints the line of code to the console, executes it, then optionally outputs one or two results.

This is often used in example code to show the specific line of code getting executed, possibly followed by the values of one or two variables that were changed by that code.

Version Description
RUN(code) Prints a stringifed code to the screen and then executes it
RUN(code,u) Follows that with a result line showing the value for u.
RUN(code,u,v) Follows that with a result line showing the values for u and v.

◆ STRINGISE

#define STRINGISE ( s)
Value:
stringise_literal(STRINGISE_IMPL(s))

STRINGISE invokes the pre-processor stringising operator, fully expanding any macro argument first!

Example

#define FOO1 "abc"
#define BAR1 FOO1
auto s = STRINGISE(BAR1);
assert(std::strcmp(s, "abc") == 0, "s = {}", s);
#define STRINGISE(s)
STRINGISE invokes the pre-processor stringising operator, fully expanding any macro argument first!
Definition macros.h:60

◆ VERSION_STRING

#define VERSION_STRING ( ...)
Value:
OVERLOAD(VERSION_STRING, __VA_ARGS__)
#define VERSION_STRING(...)
VERSION_STRING creates a semantic version string from 1, 2, or 3 arguments.
Definition macros.h:90

VERSION_STRING creates a semantic version string from 1, 2, or 3 arguments.

Example

#define major 3
#define minor 2
#define patch 1
auto s3 = VERSION_STRING(major, minor, patch);
auto s2 = VERSION_STRING(major, minor);
auto s1 = VERSION_STRING(major);
assert(std::strcmp(s3, "3.2.1") == 0);
assert(std::strcmp(s2, "3.2") == 0);
assert(std::strcmp(s1, "3") == 0);