As I know there are two ways to declare a constant variable visible only in one file:
- Declare
static const int VARIABLE = 1 - Declare it in a unnamed namespace:
namespace { const int VARIABLE = 1; }
So what's the difference?
As I know there are two ways to declare a constant variable visible only in one file:
static const int VARIABLE = 1namespace { const int VARIABLE = 1; }
So what's the difference?
Since it's const, then both have internal linkage, and there is no difference. So let's consider the more interesting case where it's not const.
In that case, then practically, there's little difference.
Technically, the first would have internal linkage, so the name can't be accessed from another translation unit; the second would have external linkage, but can't be accessed from another translation unit since its surrounding namespace can't be named.
const not declared extern), they will actually both have internal linkage...static in addition to namespace { simply as a hint to the linker. I'm not confident that it's smart enough to realize the contents of unnamed namespaces are inaccessible elsewhere, and my links take so long that I'm willing to grasp at straws.