I keep seeing code like this everywhere in my company:
namespace {
const MAX_LIMIT = 50;
const std::string TOKEN = "Token";
}
I am confused as of why you need an anonymous namespace here. On one hand, you want a local translation unit for MAX_LIMIT
AND TOKEN
. But that is already achieved without the anonymous namespace due to the const
. static const
and simple const
both achieve local translation unit.
On the other hand, you don't have naming clashes if somewhere in your file you have a variable named the same.
int foo()
{
std::string TOKEN = "MyToken"; // Clash! ::TOKEN vs TOKEN can be used.
}
That would justify the anonymous namespace. But how often you you need a variable name in your function that is actually taken already by a const
variable declared outside your function? My answer is never. So in practice, for me the unnamed namespace is not needed. Any hints?