4

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.

1
  • C++11 I'd use a strongly typed enum Commented Jan 28, 2015 at 13:41

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

3

These two are 100% equivalent, and they're also 100% equivalent to omitting both namespace and static:

const uint32_t Register1 = (0x00000000); const uint32_t Register2 = (0x00000004); const uint32_t Register3 = (0x00000008); const uint32_t Register4 = (0x0000000c); 

The reason is simple - a const variable is static unless you explicitly declare it extern.

However, this looks like something where using an enumeration would be preferable.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.