C 中的 Vector 和 < > 是什么?
What are Vectors and < > in C?
我正在查看 gcc 的源代码(出于好奇),我注意到一个我以前从未在 C 语言中见过的数据结构。
在解析器的第 80 and 129 行(以及许多其他地方),他们似乎在使用向量。
80: vec<tree> incomplete_record_decls;
129: ridpointers = ggc_cleared_vec_alloc<tree> ((int) RID_MAX);
我从未在 C 语言中遇到过这种数据类型,也没有遇到过这些:< >
。它们是 C 语言的原生语言吗?
有人知道它们是什么以及如何使用吗?
尽管文件名为 .c,但此代码在 C 中无效;它是 C++,使用该语言的 template 特性。如果你检查gcc的构建过程,你会发现这个文件实际上是用C++编译器编译的。
https://gcc.gnu.org/codingconventions.html
The directories gcc, libcpp and fixincludes may use C++03. They may also use the long long type if the host C++ compiler supports it. These directories should use reasonably portable parts of C++03, so that it is possible to build GCC with C++ compilers other than GCC itself. If testing reveals that reasonably recent versions of non-GCC C++ compilers cannot compile GCC, then GCC code should be adjusted accordingly. (Avoiding unusual language constructs helps immensely.) Furthermore, these directories should also be compatible with C++11.
请记住,尽管编译器通常会默认 从源文件的文件名推断出源文件的语言,但此默认值始终可以被覆盖。完全有可能在 .c 文件中包含 C++ 代码,或者在 .bas 文件中包含 C 代码;您可能只需要以其他方式告诉编译器正在使用什么语言。
我希望gcc选择这个文件命名约定是因为这段代码最初是用C编写的,后来转换为C++,他们发现更改所有文件名太痛苦了。更新所有 makefile 等将意味着大量工作。仅更改使用的编译器并向所有开发人员解释约定可能不那么痛苦。当然,一般来说,以标准方式命名文件是更好的编程习惯,但显然 gcc 开发人员认为在这种情况下这不是最佳做法。
自 GCC 4.8 以来,GCC 已从 C 迁移到 C++
GCC now uses C++ as its implementation language. This means that to build GCC from sources, you will need a C++ compiler that understands C++ 2003. For more details on the rationale and specific changes, please refer to the C++ conversion page.
这项工作其实早就开始了,creation of gcc-in-cxx
branch. The developers first tried to compile the source code with a C++ compiler, so there weren't any name changes. I guess they didn't bother to rename the files later when merging the two branches正式只有一个C++分支
您可以阅读GCC's move to C++了解更多历史信息
我正在查看 gcc 的源代码(出于好奇),我注意到一个我以前从未在 C 语言中见过的数据结构。
在解析器的第 80 and 129 行(以及许多其他地方),他们似乎在使用向量。
80: vec<tree> incomplete_record_decls;
129: ridpointers = ggc_cleared_vec_alloc<tree> ((int) RID_MAX);
我从未在 C 语言中遇到过这种数据类型,也没有遇到过这些:< >
。它们是 C 语言的原生语言吗?
有人知道它们是什么以及如何使用吗?
尽管文件名为 .c,但此代码在 C 中无效;它是 C++,使用该语言的 template 特性。如果你检查gcc的构建过程,你会发现这个文件实际上是用C++编译器编译的。
https://gcc.gnu.org/codingconventions.html
The directories gcc, libcpp and fixincludes may use C++03. They may also use the long long type if the host C++ compiler supports it. These directories should use reasonably portable parts of C++03, so that it is possible to build GCC with C++ compilers other than GCC itself. If testing reveals that reasonably recent versions of non-GCC C++ compilers cannot compile GCC, then GCC code should be adjusted accordingly. (Avoiding unusual language constructs helps immensely.) Furthermore, these directories should also be compatible with C++11.
请记住,尽管编译器通常会默认 从源文件的文件名推断出源文件的语言,但此默认值始终可以被覆盖。完全有可能在 .c 文件中包含 C++ 代码,或者在 .bas 文件中包含 C 代码;您可能只需要以其他方式告诉编译器正在使用什么语言。
我希望gcc选择这个文件命名约定是因为这段代码最初是用C编写的,后来转换为C++,他们发现更改所有文件名太痛苦了。更新所有 makefile 等将意味着大量工作。仅更改使用的编译器并向所有开发人员解释约定可能不那么痛苦。当然,一般来说,以标准方式命名文件是更好的编程习惯,但显然 gcc 开发人员认为在这种情况下这不是最佳做法。
自 GCC 4.8 以来,GCC 已从 C 迁移到 C++
GCC now uses C++ as its implementation language. This means that to build GCC from sources, you will need a C++ compiler that understands C++ 2003. For more details on the rationale and specific changes, please refer to the C++ conversion page.
这项工作其实早就开始了,creation of gcc-in-cxx
branch. The developers first tried to compile the source code with a C++ compiler, so there weren't any name changes. I guess they didn't bother to rename the files later when merging the two branches正式只有一个C++分支
您可以阅读GCC's move to C++了解更多历史信息