Live data from Hacker News

C++ is not a superset of C

mcla.ug

31–40 of 114 posts

Re: C++ is not a superset of C

#33
post #32

That pink background to the left weighs 6 MB. Do you really think it's necessary?

I should probably make it smaller! 6MB is pretty indulgent. But I do like how it looks and this is mostly just a fun blog for myself :)

Re: C++ is not a superset of C

#34

Nearly everything that is mentioned in this blog is a subject of change / addition to the latest C standard, code named C2x. They are in discussions to introduce the following in C: * nullptr * auto * __has_include * make false and true first-class language features * constexpr and lots of other goodies [1]. https://gustedt.wordpress.com/2018/11/12/c2x/

Are they trying to make C literally become the same as C++ except classes and templates?

Re: C++ is not a superset of C

#35

The code: const size_t buffer_size = 5; int buffer[buffer_size]; compiles fine in c, but not for the same reason. C99/C11 has dynamic arrays https://en.wikipedia.org/wiki/Variable-length_array#C99

Yes, I think I made an embarrassing mess up here by using a compiler that doesn't support VLAs, and not fully understanding VLAs. Thanks for pointing it out.

Re: C++ is not a superset of C

#36
Designated initializers were added in c++20, I was just looking at a recent change to clang's semantic analysis that warns on the differences between C99 and C++20 designated initializers (Todo: post link when not mobile.)

One recent difference I saw was valid in (via GNU C extension) but invalid in c++:

    struct foo my_foo = ({
      init(&my_foo);
      my_foo;
    });

Re: C++ is not a superset of C

#38
post #10

Several of the examples shown as valid C are not. For example, this: const int foo = 1; int* bar = &foo; *bar = 2; is said to have undefined behavior, but in fact the initialization of `bar` is a constraint violation, requiring a diagnostic. (Some compilers will issue a non-fatal warning, which is allowed by the C standard but IMHO is unfortunate.) Another example: it says that this: const size_t buffer_size = 5; int…

> Some compilers will issue a non-fatal warning, which is allowed by the C standard but IMHO is unfortunate.

GCC 5.1.0 reports:

  warning: initialization discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]

Re: C++ is not a superset of C

#39
The designated initializers example will actually compile in recent versions of GCC, Clang, and Visual C++, even if that's not part of the standard prior to C++20. A better example (i.e. one that doesn't compile in C++ but does in C) would be

  int arr[3] = { [1] = 5 }
or even

  struct A c = {.x = 1, 2}
or another example where the designators are not declared in order of the struct members, or where the designators are nested. The version of designated initializers standardized in C++20 only allows the simple case that's currently implemented in all the major compilers.

See https://stackoverflow.com/a/29337570 for more info and examples.

Re: C++ is not a superset of C

#40

I never heard anyone say that C++ is a superset of C. Sure, the first version was a preprocessor on top of C and certainly that is common knowledge. But a superset? Never heard it. ObjC on the other hand...

FWIW, https://duckduckgo.com/?q=%22C%2B%2B+is+a+superset+of+C%22&t... finds things like:

- C++ is a superset of C, and that virtually any legal C program is a legal C++ program. https://www.tutorialspoint.com/cplusplus/cpp_overview.htm

- "C is a subset of C++." / "C++ is a superset of C." https://www.geeksforgeeks.org/difference-between-c-and-c/

- "C++ is a superset of C; (almost) anything you can do in C, you can do in C++." https://www.cprogramming.com/begin.html

- "As you recall C is a root for C++ and C++ is a superset of C." https://www.c-sharpcorner.com/article/similarities-and-diffe...

Post reply on HN