constexpr size_t N = 12;
#define N 12
constexpr is modern way of doing things but in terms of efficiency what
is the best way to do?
On 08/20/24 6:30 PM, Jack wrote:
constexpr size_t N = 12;
#define N 12
constexpr is modern way of doing things but in terms of efficiency what
is the best way to do?
There's no difference in efficiency.
One can probably come up with a few reasons to prefer a macro for this purpose, like
1. Compatibility with C in cross-compiled header files
2. Ability to use a named constant in subsequent preprocessor directives (e.g. `#if`)
3. Probably something else...
But these reasons are focused rather narrowly of some niche
applications. When you _have_ a choice, there's no reason to avoid `constexpr`.
Keith Thompson wrote this copyrighted missive and expects royalties:
<brevsnip>
I believe the general consensus (and my own preference) is to use core
language features rather than macros unless there's a very good reason
to use a macro.
I use macros for enabling/disabling code depending on developer preference or >platform.
Is that a sensible use of macros? (A little off-topic, sorry).
<brevsnip>
I believe the general consensus (and my own preference) is to use core language features rather than macros unless there's a very good reason
to use a macro.
Chris Ahlstrom <[email protected]> writes:
Keith Thompson wrote this copyrighted missive and expects royalties:
<brevsnip>
I believe the general consensus (and my own preference) is to use core
language features rather than macros unless there's a very good reason
to use a macro.
I use macros for enabling/disabling code depending on developer preference or
platform.
Is that a sensible use of macros? (A little off-topic, sorry).
Generally speaking, that's sensible.
However it can be taken to an extreme that makes maintenance of
the application more complicated. Every conditional used to
select optional code needs to be tested combinitorially to ensure
proper operation of the application, which can consume considerable
QA time. If N is the number of macros, then there are as many
as 2^n (or more for non-boolean conditionals) different compiles
required for full verification of the application (just to ensure
that it compiles sucessfully).
Maintenance can be very difficult when there are an excessive number of conditionally compiled segments in a codebase.
On 22/08/2024 19:54, Scott Lurndal wrote:
Chris Ahlstrom <[email protected]> writes:
Keith Thompson wrote this copyrighted missive and expects royalties:
<brevsnip>
I believe the general consensus (and my own preference) is to use core >>>> language features rather than macros unless there's a very good reason >>>> to use a macro.
I use macros for enabling/disabling code depending on developer preference or
platform.
Is that a sensible use of macros? (A little off-topic, sorry).
Generally speaking, that's sensible.
However it can be taken to an extreme that makes maintenance of
the application more complicated. Every conditional used to
select optional code needs to be tested combinitorially to ensure
proper operation of the application, which can consume considerable
QA time. If N is the number of macros, then there are as many
as 2^n (or more for non-boolean conditionals) different compiles
required for full verification of the application (just to ensure
that it compiles sucessfully).
Maintenance can be very difficult when there are an excessive number of
conditionally compiled segments in a codebase.
Usually the number of valid combinations is not nearly that many, as
they are rarely independent. (And if they are truly independent, then
they can often be tested independently.) But you are right that too
many of them can make code hard to handle in many aspects.
| Sysop: | Keyop |
|---|---|
| Location: | Huddersfield, West Yorkshire, UK |
| Users: | 715 |
| Nodes: | 16 (0 / 16) |
| Uptime: | 161:49:07 |
| Calls: | 12,094 |
| Calls today: | 2 |
| Files: | 15,000 |
| Messages: | 6,517,778 |