I have some inline functions contained within a namespace in a header file and am not currently in a position to move them into a cpp file. Some of these inline functions use magic constants, for example:
// Foo.h namespace Foo { const int BAR = 1234; inline void someFunc() { // Do something with BAR } }
However, I want to make these magic constants private - any ideas how? My first thought was to use an anonymous namespace thus:
// Foo.h namespace Foo { namespace { // 'private' constants here const int BAR = 1234; } inline void someFunc() { // Do something with BAR } }
However, this doesn't work and Foo::BAR
is available to any cpp file that includes Foo.h
? Is there a way to do this without creating an implementation cpp file?
You can't, anonymous namespaces work for the translation unit they are defined in (or included into in your case).
You could consider moving them into a detail
namespace to signal to the user that they are internal details:
namespace foo { namespace detail { int magic = 42; } // ... use detail::magic }
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