匿名namespace,linkage,non-type argument

本文详细解释了C和C++编程语言中的内部链接与外部链接的概念。内部链接意味着标识符仅在其被编译的文件中可见,而外部链接则使得标识符在所有被编译的文件中共享同一存储空间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

好久没有更新了。收集一下最近看过的几个问题的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而已。(一直在误解中...才明白)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值