1

我尝试在 openstd org 上谷歌并快速搜索“C lang”和“C18”的最新草案。C++ 标准会支持最新的 C 标准吗?

4

3 回答 3

3

C++ 是一种基于ISO/IEC 9899:2018编程语言 — C(以下简称 C 标准)中描述的 C 编程语言的通用编程语言。

C++ 提供了许多 C 以外的功能,包括附加的数据类型、类、模板、异常、命名空间、运算符重载、函数名重载、引用、自由存储管理运算符和附加的库设施。

http://eel.is/c++draft/intro.scope

C18(以前称为 C17)是ISO/IEC 9899:2018的非正式名称,它是 C 编程语言的最新标准,于 2018 年 6 月发布。它取代了 C11(标准 ISO/IEC 9899:2011)。

https://en.m.wikipedia.org/wiki/C18_(C_standard_revision)

于 2020-03-05T11:57:01.687 回答
2

C++(任何版本)不包括 C(任何版本)批发。它仅根据需要引用 C 规范的部分内容。例如,C++ 包括(大部分)C 标准库,它通过引用 C 标准的适当部分而不是从它复制来做到这一点。

当 C++20 引用 C 规范的一个版本时,它引用了 C18。

于 2020-03-05T14:31:49.370 回答
0

我相信您的问题背后的原因是,当您extern "C"在 C++ 中使用时,它会以某种方式调用特定版本的单独 C 编译器。

它没有。什么extern "C"是告诉 C++ 编译器对函数使用 C 链接,以便使用 C 链接的其他代码可以正确链接到这些函数。它不会影响源代码的编译方式,除了如果您尝试在extern块内重载非成员函数会引发编译器错误。

如果你写这样的东西,C++ 编译器不会抱怨:

extern "C" {

    // this is still a C++ compiler, works as usual
    class CPP
    {
        public:

            // these are inside a class and can be overloaded,
            // and they will be mangled as usual
            static int foo(int i) { return i; };
            static int foo(int i, int j) { return i + j; }
    };

    // these will not be mangled due to 'extern "C"'        
    int foo(int i) { return CPP::foo(i); }
    int bar(int i, int j) { return CPP::foo(i, j); }
}

同时,这个简单的 C 代码在任何 C++ 编译器中都会失败:

int * x = malloc(1);

与 C11 中的这段代码一样,因为_Atomic它不是 C++ 标准中的有效限定符:

#include <stdatomic.h>    
_Atomic int x;
于 2020-03-05T13:45:02.823 回答