anonymous namespace

后端 未结 5 491
南旧
南旧 2020-12-25 14:03

What is the different between these two?

cpp-file:

namespace
{
    int var;
}

or

int var;

if both

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-25 14:43

    Two points:

    1. anyone using extern int var; can access your variable if it's not in an unnamed namespace.
    2. if in another file, you have another int var global variable, you will have multiple definitions of this variable.

    As specified in the standard :

    [...] all occurrences of unique in a translation unit are replaced by the same identifier and this identifier differs from all other identifiers in the entire program.

提交回复
热议问题