为什么 C++ 中允许多个 const 全局变量定义而不是在C语言中?
由于单一定义规则,C 或 C++ 中不允许对全局变量进行多重定义。但是,在 C++ 中,可以在多个编译单元中定义 const 全局变量,而不会出现错误。这与 C 中的不一样。
为什么 C++ 允许这样做,而 C 不允许?与 C 相比,为什么 C++ 中 const 全局变量的用法和行为与非常量全局变量如此不同? C++ 和 C 中关于 const 的幕后发生了什么?
例如,这在 C++ 中是允许的,但在 C 中是错误的:
// Foo.cpp
const int Foo = 99;
// Main.cpp
const int Foo = 99;
int main()
{
cout << Foo << endl;
return 0;
}
这对于 C 来说没问题,但在 C++ 中是错误的:
// Foo.cpp
const int Foo = 99;
// Main.cpp
extern const int Foo;
int main()
{
cout << Foo << endl;
return 0;
}
Multiple definition of a global variable is not allowed in C or C++ due to the One Definition Rule. However, in C++ a const global variable can be defined in multiple compilation units with no error. This is not the same as in C.
Why does C++ allow this while C does not? Why does the usage and behaviour of a const global differ from a non-const global in this way in C++ compared to C? What is happening under the covers with C++ and C with respect to const?
For example this is allowed in C++, but wrong in C:
// Foo.cpp
const int Foo = 99;
// Main.cpp
const int Foo = 99;
int main()
{
cout << Foo << endl;
return 0;
}
And this is fine with C, but wrong with C++:
// Foo.cpp
const int Foo = 99;
// Main.cpp
extern const int Foo;
int main()
{
cout << Foo << endl;
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
命名空间范围内的
const
变量具有内部链接。所以它们基本上是两个不同的变量。没有重新定义。来自@David 的评论,3.5/3 [basic.link]:
在第二种情况下,你应该这样做(正确的方法):
const
variable at namespace scope has internal linkage. So they're basically two different variables. There is no redefinition.From @David's comment, 3.5/3 [basic.link]:
In the second case, you should be doing this (correct way):
我认为您是在询问基本原理,而不是允许这样做的特定语言规则。
这样做的理由是它使 const 变量更容易使用。它为
#define
的一种常见用法提供了类型化替代。您可以以完全相同的方式使用
const int max_count = 211;
,而不是#define MAX_COUNT 211
,例如共享头文件,而不必担心将一个定义。您无法合法地更改
const
对象的值,因此具有相同值的一个对象和多个对象之间没有明显的区别。由于您可以将 const 对象的定义放入头文件中,因此编译器可以轻松地在编译阶段直接使用该值,而无需将此类优化延迟到链接时修复。
I think you are asking for the rationale and not the specific language rule that allows this.
The rationale for this is that it makes
const
variables much easier to use. It gives a typed replacement for one common use of#define
.Instead of
#define MAX_COUNT 211
you can useconst int max_count = 211;
in exactly the same way, e.g. a shared header file, without having to worry about where to put the one definition.You can't legally change the value of a
const
object so there's no visible difference between having one object and multiple objects with the same value.As you can put a definition of a
const
object in a header file it makes trivial for the compiler to use the value directly at the compilation stage without such optimizations having to be delayed to a link-time fixup.基本上,在 C++ 中,const、非局部变量是真正的常量表达式,或 constexpr。这允许做很多事情,比如 TMP。
在C中,它们只是一个不能修改的变量。也就是说,
完全等同于
有效地,C++ 将某些类型的
const
变量提升为新类别constexpr
,而在 C 中,这不存在,它们只是变量这恰好是不可修改的。Basically, in C++, const, non-local variables are genuine constant expressions, or constexpr. This permits plenty of things, like TMP.
In C, they are just a variable that cannot be modified. That is,
Is quite equivalent to
Effectively, C++ promotes some kinds of
const
variable to a new category,constexpr
, whereas in C, this does not exist and they are just variables which happen to be unmodifiable.它看起来像 const 实际上并不生成外部符号。
It looks like const doesn't actually generate an external symbol.
为什么英国人拼写COLOUR,而美国人拼写COLOR?
它们是来自同一基础的两种不同语言,但它们没有相同的规则。
C& C++也是一样的。如果它们没有不同,它们就会被称为同一个东西。
Why do English people spell COLOUR, whereas American people spells it COLOR?
They are 2 different languages from the same base, but they don't have the same rules.
C & C++ are the same. If they weren't different, they would both be called the same thing.
我的解决方法是将其声明为:
它适用于我的情况。
My workaround was declaring it as :
it worked in my situation.