1

I tried to google and quick search on latest draft for "C lang" and "C18" on openstd org. Will C++ standard support the latest standards of C?

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • 7
    No, for instance C++20 won't have the infamous VLAs. – Blaze Mar 05 '20 at 11:10
  • But will it support Designated initialisers? – Pinus Growlwards Mar 05 '20 at 11:14
  • 1
    [Yes it will](https://en.wikipedia.org/wiki/C%2B%2B20). – Marek R Mar 05 '20 at 11:38
  • 4
    @Blaze: The C++ standard includes certain other standards by reference, including the C standard. It does this to make definitions and specifications from those documents part of the C++ standard, such as behaviors of library routines. It does not mean the C++ language has all the features or behaviors of the C language. – Eric Postpischil Mar 05 '20 at 12:32
  • 1
    All the latest compilers in C++20 accept designated initializers: https://godbolt.org/z/mrrxLi – Robert Andrzejuk Mar 05 '20 at 12:33
  • 2
    No C++ standard to date has completely supported the latest (relative to it) C standard. There tends to be a substantial subset of C, but not the whole of C. For example, no C++ standard has yet supported an equivalent of C's `restrict` keyword (although some compilers do support a similar feature, with different names such as `__restrict` or `__restrict__` as an extension). I would be quite surprised if any C++ standard ever supported all of standard C as a subset. – Peter Mar 05 '20 at 13:14
  • 1
    @MarekR: "*Yes it will.*" No, it won't. It supports a *subset* of C's designated initializers. There's a big difference. – Nicol Bolas Mar 05 '20 at 14:21
  • @RobertAndrzejuk - what happens if you change the `S a{.i = 1, .b = false};` to `S a{ .b = false, .i = 1};` (i.e. swap the order of the designated initialisers so it doesn't match the order in which members are declared in the type `S`). Reason I ask is that one of the documented incompatibilities between C++ and C designated initialisers is that C++ does not allow initialisers in arbitrary order, but C does. – Peter Mar 05 '20 at 14:32
  • @Peter You can try it out yourself on the linked Godbolt site. 2 compilers give errors, 1 gives a warning. – Robert Andrzejuk Mar 05 '20 at 14:44
  • @RobertAndrzejuk Well, those are anecdotal results, not something we could rely on as specified in a standard. To my knowledge, the order must match. – Bob__ Mar 05 '20 at 15:33
  • @Bob__ You are correct. But Peter's question was "what happens if... ". – Robert Andrzejuk Mar 05 '20 at 15:55

3 Answers3

3

C++ is a general purpose programming language based on the C programming language as described in ISO/IEC 9899:2018 Programming languages — C (hereinafter referred to as the C standard).

C++ provides many facilities beyond those provided by C, including additional data types, classes, templates, exceptions, namespaces, operator overloading, function name overloading, references, free store management operators, and additional library facilities.

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

C18 (previously known as C17) is the informal name for ISO/IEC 9899:2018, the most recent standard for the C programming language, published in June 2018. It replaced C11 (standard ISO/IEC 9899:2011).

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

Community
  • 1
  • 1
Robert Andrzejuk
  • 5,076
  • 2
  • 22
  • 31
  • So, that means yes. If standard document the same and "C++ is a general-purpose programming language based on the C programming language", C++ developers will always have all nice C perks. But then why there's no support for Designated initialisers now (C++17 and below)? Is it a compiler problem? – Pinus Growlwards Mar 05 '20 at 12:46
  • 1
    For C++17: " *C++ is a general purpose programming language **based** on the C programming language as described inISO/IEC 9899:2011 Programming languages — C* ". Please be careful, it does not say that it fully subsumes the C standard. – Robert Andrzejuk Mar 05 '20 at 12:55
  • 1
    In [Normative references](http://eel.is/c++draft/intro.refs) it says: *The following documents are referred to in the text in such a way that **some or all** of their content constitutes requirements of this document.* – Robert Andrzejuk Mar 05 '20 at 12:59
  • Thank you! Maybe there is a place where one can find what exact features C++ has from C (and what does not)? I mean, not manually reading standard, but look at an overview. – Pinus Growlwards Mar 05 '20 at 16:12
  • Thank you! Though I mean document (standard)-wise, not compiler-wise. Theory. – Pinus Growlwards Mar 05 '20 at 18:28
2

C++ (of any version) does not include C (of any version) wholesale. It merely references parts of the C specification as needed. For example, C++ includes (most) of the C standard library, and it does so by referencing the appropriate parts of the C standard instead of copying from it.

When C++20 references a version of the C specification, it references C18.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • But isn't both included in one standard described in ISO/IEC 9899:2018. Where do I find what exactly C++ has from C. – Pinus Growlwards Mar 05 '20 at 16:10
  • 1
    @PinusGrowlwards: "*But isn't both included in one standard described in ISO/IEC 9899:2018.*" No. "*Where do I find what exactly C++ has from C.*" You really can't, and you shouldn't try. For example, C++20 does not have C's designated initializers; it has its own designated initializer syntax which is compatible with C's, but C's syntax has features that C++20's does not. C++ often subtly changes the meaning of stuff it adopts from C to fit its own needs and object model. – Nicol Bolas Mar 05 '20 at 16:12
0

I believe the reasoning behind your question is that, when you use extern "C" in C++, it somehow invokes a separate C compiler of a specific version.

It doesn't. What extern "C" does is tell the C++ compiler to use C linkage for the functions so that other code using C linkage can properly link to these functions. It doesn't affect how the source code is compiled, apart from throwing compiler errors if you try to overload non-member functions inside the extern block.

The C++ compiler will not complain a bit if you write something like this:

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); }
}

At the same time this simple C code will fail in any C++ compiler:

int * x = malloc(1);

As will this code from C11, since _Atomic is not a valid qualifier in the C++ standard:

#include <stdatomic.h>    
_Atomic int x;
vgru
  • 49,838
  • 16
  • 120
  • 201
  • While this answer is perfectly accurate, it looks like you've posted it to the wrong question :) – Quentin Mar 05 '20 at 15:06
  • 1
    @Quentin: It doesn't directly answer the question, but this is indeed how I interpreted OP's "XY problem", because I don't see any other reasoning behind "does C++ support a certain version of C". No, no version of C++ "supports" no version of C. You cannot even write `int * x = malloc(sizeof *x)` in C++, let alone declare `_Atomic bool x;` from C11. Anyway, perhaps somebody will find this answer useful so I'll leave it for now. – vgru Mar 05 '20 at 15:29
  • This answer has value. Thank you. The reasoning isn't like that. I want to know what makes me aware that _Atomic is not a valid qualifier (not running the compiler). – Pinus Growlwards Mar 05 '20 at 16:20