Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use static or a namespace?

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.

like image 576
user1876942 Avatar asked Feb 12 '23 01:02

user1876942


1 Answers

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.

like image 85
Lightness Races in Orbit Avatar answered Feb 14 '23 15:02

Lightness Races in Orbit