I have a dedicated HW register header file, I have created a namespace, like this, which holds all of my HW register addresses:
namespace{
const uint32_t Register1 = (0x00000000);
const uint32_t Register2 = (0x00000004);
const uint32_t Register3 = (0x00000008);
const uint32_t Register4 = (0x0000000c);
}
Is this considered better than using:
static const uint32_t Register1 = (0x00000000);
static const uint32_t Register2 = (0x00000004);
static const uint32_t Register3 = (0x00000008);
static const uint32_t Register4 = (0x0000000c);
I guess the point of namespaces is that we don't pollute the global namespace. Is that right?
I have one .cpp, which uses the header file.
The two are essentially equivalent.
The global-static
method was deprecated in C++03 ([depr.static]
) in favour of unnamed namespaces, but then undeprecated by C++11 because everybody realised there is no objective benefit of one over the other in the general case.
However, for this, you may find an enum
or enum class
to be more manageable and idiomatic.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With