In your second case, when you don't use an anonymous namespace, if any other cpp file declares an extern int var;
, it will be able to use your variable.
If you use an anonymous namespace, then at link time, the other cpp file will generate an undefined reference error.