I have the following problem. In a .hpp file I have a function Space::foo() that must use a namespace global variable, I thought of hiding it in an anonymous namespace. But now I wonder, do I risk having values defined in each translation unit with multiple copies or there will only be one? Thanks for helping. This comes from the fact that I needed a bunch of functions in the Space namespace with a sort of private data part (the anonymous namespace stuff) so that Space is sort of like a class with only static members, but I don't want to have multiple copies of these variables if I include the .hpp file in different units.
// .hpp file
#include <array>
namespace Space {
namespace {
constexpr std::array<int, 10000> fill() { /* ... */ };
inline constexpr std::array<int, 10000> values = fill();
}
inline int foo(int i) {
return values[i];
}
}