diff options
author | Thomas Habets <habets@google.com> | 2019-11-20 12:30:19 +0000 |
---|---|---|
committer | Martin Braun <martin.braun@ettus.com> | 2020-01-04 22:35:12 -0800 |
commit | 27a466a9c8a03392270fadde9e5bafef5c062e61 (patch) | |
tree | b7b8ce04ef2b95cb663fc06faad4a866062007e6 /gr-digital/include | |
parent | 8c6657f5edad34f3a1058a676f737536be16ce20 (diff) |
Replace many `const` variables with `constexpr`
constexpr is like const but (for variables) guarantees evaluation at
compile time (as opposed to runtime).
Likely this change will do nothing on its own (though it could, since
it gives the compiler more information). But it still has benefits.
It allows programmer to know that initialization is not expensive (it
was done at compile time), and reduces risk of a refactoring
regressing the compiletimeness.
Runtime initialization can be nonobvious in larger codebases. E.g.:
struct S {
static int foo();
};
const int bar = S::foo(); // Called and initialized at *runtime*.
int S::foo() { return 10; }
With constexpr:
struct S {
static constexpr int foo();
};
constexpr int bar = S::foo(); // Error: used before definition.
constexpr int S::foo() { return 10; }
Initializing at runtime is not just startup costs, but also can save
memory since it'll end up in a R/O section of a binary and therefore
doesn't need to be swapped out, but can be shared (in the mmap() sense
of the word).
Diffstat (limited to 'gr-digital/include')
-rw-r--r-- | gr-digital/include/gnuradio/digital/simple_framer_sync.h | 12 |
1 files changed, 6 insertions, 6 deletions
diff --git a/gr-digital/include/gnuradio/digital/simple_framer_sync.h b/gr-digital/include/gnuradio/digital/simple_framer_sync.h index 532a9937c0..49501ef795 100644 --- a/gr-digital/include/gnuradio/digital/simple_framer_sync.h +++ b/gr-digital/include/gnuradio/digital/simple_framer_sync.h @@ -42,13 +42,13 @@ namespace digital { * 0xACDDA4E2F28C20FC (padded on right with a zero) * </pre> */ -static const unsigned long long GRSF_SYNC = 0xacdda4e2f28c20fcULL; +static constexpr unsigned long long GRSF_SYNC = 0xacdda4e2f28c20fcULL; -static const int GRSF_BITS_PER_BYTE = 8; -static const int GRSF_SYNC_OVERHEAD = sizeof(GRSF_SYNC); -static const int GRSF_PAYLOAD_OVERHEAD = 1; // 1 byte seqno -static const int GRSF_TAIL_PAD = 1; // one byte trailing padding -static const int GRSF_OVERHEAD = +static constexpr int GRSF_BITS_PER_BYTE = 8; +static constexpr int GRSF_SYNC_OVERHEAD = sizeof(GRSF_SYNC); +static constexpr int GRSF_PAYLOAD_OVERHEAD = 1; // 1 byte seqno +static constexpr int GRSF_TAIL_PAD = 1; // one byte trailing padding +static constexpr int GRSF_OVERHEAD = GRSF_SYNC_OVERHEAD + GRSF_PAYLOAD_OVERHEAD + GRSF_TAIL_PAD; } /* namespace digital */ |