anonymous namespace

后端 未结 5 493
南旧
南旧 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:26

    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.

提交回复
热议问题