现在的位置: 首页 > 综合 > 正文

匿名namespace,linkage,non-type argument

2013年09月15日 ⁄ 综合 ⁄ 共 1860字 ⁄ 字号 评论关闭

好久没有更新了。收集一下最近看过的几个问题的link

关于匿名namespace的作用

http://bbs.ustc.edu.cn/cgi/bbsgcon?bn=CPlusPlus&fn=G460B6463&num=326

关于里面使用了 non-type argument来测试linkage的作法,因为是需要编译时就知道值的所以参数必须是external linkage的

关于external linkage,internal linkage,no linkage区别可以看下面的文字

Linkage
To understand the behavior of C and C++ programs, you need to know about linkage. In an executing program, an identifier is represented by storage in memory that holds a variable or a compiled function body. Linkage describes this storage as it is seen by the linker. There are two types of linkage: internal linkage and external linkage.

Internal linkage means that storage is created to represent the identifier only for the file being compiled. Other files may use the same identifier name with internal linkage, or for a global variable, and no conflicts will be found by the linker – separate storage is created for each identifier. Internal linkage is specified by the keyword static in C and C++.

External linkage means that a single piece of storage is created to represent the identifier for all files being compiled. The storage is created once, and the linker must resolve all other references to that storage. Global variables and function names have external linkage. These are accessed from other files by declaring them with the keyword extern. Variables defined outside all functions (with the exception of const in C++) and function definitions default to external linkage. You can specifically force them to have internal linkage using the static keyword. You can explicitly state that an identifier has external linkage by defining it with the extern keyword. Defining a variable or function with extern is not necessary in C, but it is sometimes necessary for const in C++.

Automatic (local) variables exist only temporarily, on the stack, while a function is being called. The linker doesn’t know about automatic variables, and so these have no linkage.

Go confidently in the  directions of your dreams,live the life you have imagined!Just do it!
It is no use learning without thinking!

注意全局变量,函数除了const外都是external的。const是internal的

我们在头文件中加上了extern的关键字只是为了表示是declare而不是define而已。(一直在误解中...才明白)

抱歉!评论已关闭.