为什么const暗示内部链接在C ++中,当它不在C? [英] Why does const imply internal linkage in C++, when it doesn't in C?
问题描述
查看主题。他们在想什么?
更新:从静态更改为内部链接,以避免混淆。
给出一个例子...将以下内容放在一个文件中:
const int var_a = 1;
int var_b = 1;
...并使用 g ++ -c test.cpp
只导出 var_b
。
为什么const表示C ++中的内部链接
这是真的,如果你声明一个const对象在命名空间范围,那么它有内部链接。
附录C说明理由
更改:显式声明为const的文件范围的名称,而不是显式声明的extern,具有内部链接,而在C中它有外部链接
由于const对象可以用作C ++中的编译时值,此功能促使程序员为每个const提供显式初始化值。此功能允许用户将const对象放在许多编译单元中包含的头文件中。
See subject. What were they thinking?
UPDATE: Changed from "static" to "internal linkage" to save confusion.
To give an example... Putting the following in a file:
const int var_a = 1;
int var_b = 1;
...and compiling with g++ -c test.cpp
only exports var_b
.
I believe you mean
Why does const imply internal linkage in C++
It's true that if you declare a const object at namespace scope, then it has internal linkage.
Appendix C gives the rationale
Change: A name of file scope that is explicitly declared const, and not explicitly declared extern, has internal linkage, while in C it would have external linkage
Rationale: Because const objects can be used as compile-time values in C++, this feature urges programmers to provide explicit initializer values for each const. This feature allows the user to put const objects in header files that are included in many compilation units.
这篇关于为什么const暗示内部链接在C ++中,当它不在C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!