Why do functions/objects inside anonymous namespace have external linkage?

前端 未结 1 1260
故里飘歌
故里飘歌 2020-12-03 01:46

Why don\'t symbols (functions and variables) that are defined in an anonymous namespace have internal linkage as with static keyword? If a function is not visible/accessible

相关标签:
1条回答
  • 2020-12-03 02:23

    In C++03, names with internal linkage were forbidden from being used as template arguments[*]. So, names of most things in unnamed namespaces had external linkage to allow their use with templates. You could explicitly give a name internal linkage in an unnamed namespace by declaring it static, same as in a named or global namespace.

    Both things changed in C++11 -- names in unnamed namespaces have internal linkage by default (3.5/4), and names with internal linkage can be used as template arguments.

    [*] for types, it must have external linkage. For objects and functions, it must have external linkage if its address is used as a template argument, although it's OK for example to use as a template argument the value of a const integer with internal linkage.

    0 讨论(0)
提交回复
热议问题