Tag Info

New answers tagged

7

When are macros idiomatic and when should they be avoided? Macros are idiomatic only when there is no alternative to their use. Examples are include guards (they are the only portable form), embedded domain-specific languages, special compiler support not available through other language features (embedding built-in macros like __FILE__, stringifying ...


3

The only time I'd consider macros are for compile time constants, typically ones that affect the compilation (as opposed to constant application values, which are better modelled as const types). However, I have modelled utility routines such as calls to logging functions in a macro just so they will be compiled out, but I think even this is unnecessary ...


10

As a rule, you should only use macros, when a better alternative does not exist. They should not be used to generate code; you should simply write the code instead (if the code is type-agnostic, write a template instead). They should not be used to define constants; constants should be defined using one of these: (static) constexpr/const variables, ...



Top 50 recent answers are included