
Update all links to C++ Core Guidelines to `e49158a`.
Refresh done using the following script and some manual edits:
db76e34e74/personal/fred-tingaud/rspec/refresh-cppcoreguidelines.py
When re-using this script, be mindful that:
- it does not cover `shared_content`
- it does not properly escape inline code in links (e.g., "[=]" or "`mutex`es")
- it does not change `C++` to `{cpp}` in link titles.
Co-authored-by: Marco Borgeaud <marco.borgeaud@sonarsource.com>
51 lines
1.5 KiB
Plaintext
51 lines
1.5 KiB
Plaintext
== Why is this an issue?
|
|
|
|
A macro is a textual replacement, which means that it's not respecting the type system, it's not respecting scoping rules... There is no reason not to use a constant instead.
|
|
|
|
|
|
Most of the time, a macro can be replaced by a ``++constexpr++`` declaration (a constant that is guaranteed to be computed during compilation). If your compiler is too old to properly handle ``++constexpr++``, you may use ``++const++`` instead.
|
|
|
|
|
|
If you have a series of related integer macros, you might also consider replacing them by an ``++enum++``.
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,cpp]
|
|
----
|
|
#define MAX_MEMORY 640 // Noncompliant
|
|
|
|
#define LEFT 0 // Noncompliant
|
|
#define RIGHT 1 // Noncompliant
|
|
#define JUMP 2 // Noncompliant
|
|
#define SHOOT 3 // Noncompliant
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,cpp]
|
|
----
|
|
constexpr size_t MAX_MEMORY = 640;
|
|
enum class Actions {Left, Right, Jump, Shoot};
|
|
----
|
|
|
|
|
|
== Resources
|
|
|
|
* {cpp} Core Guidelines - https://github.com/isocpp/CppCoreGuidelines/blob/e49158a/CppCoreGuidelines.md#es31-dont-use-macros-for-constants-or-functions[ES.31: Don't use macros for constants or "functions"]
|
|
* {cpp} Core Guidelines - https://github.com/isocpp/CppCoreGuidelines/blob/e49158a/CppCoreGuidelines.md#enum1-prefer-enumerations-over-macros[Enum.1: Prefer enumerations over macros]
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Replace this macro by "const" or "constexpr"
|
|
|
|
|
|
endif::env-github,rspecator-view[]
|