Home >Backend Development >C++ >Why Do C and C Differ in Their Treatment of Namespace-Scope `const` Objects?

Why Do C and C Differ in Their Treatment of Namespace-Scope `const` Objects?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-04 08:53:11445browse

Why Do C   and C Differ in Their Treatment of Namespace-Scope `const` Objects?

Internal Linkage with Const in C vs. C

It's a known fact that declaring a const object at namespace scope in C results in internal linkage, contrary to the behavior observed in C.

The rationale behind this distinction is explained in Appendix C of the C 11 standard (section C.1.2). It states that file-scope const objects are limited to internal linkage, even without an explicit extern declaration. This change serves two purposes:

  1. Encouraging Explicit Initializers: By restricting the linkage of const objects within the file, programmers are encouraged to explicitly specify initial values during declaration.
  2. Header File Inclusion: This feature enables the placement of const objects in header files that are included across multiple compilation units.

The above is the detailed content of Why Do C and C Differ in Their Treatment of Namespace-Scope `const` Objects?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn