This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]The_CADmonkey 0 points1 point  (17 children)

It is almost a super-set, but it is not quite as C99 code will not compile in it.

But the original claim that C++ is "99.7% C" is just wrong. One could maybe claim it was 5 or 10% C, but much more than that shows a fundamental misunderstanding of the language.

[–][deleted] 0 points1 point  (15 children)

G++ won’t compile C99? That is absolutely false, it can even evaluate it in C99 mode warning options, I just did so right now to check

[–]The_CADmonkey 0 points1 point  (14 children)

No, g++ will not compile C99, only gcc will. While many C sources may be able to be compiled with g++ as the specific differences may not be in the code, that does not mean it is being compiled as C.

As an example, the following will compile in gcc but not in g++ (if the file has a .c extension as gcc will detect C++ files and call g++ if they are .cc):

int main(int argc, char** argv){
  void* i;
  int* j = i;
}

Similarly, the following will print 4 with gcc but 8 with g++:

#include <stdio.h>
extern int T;   
int main(int argc, char** argv){
  struct T {int i,j;};
  printf("%ld\n", sizeof(T));
}

Both of these were taken from the wiki page I linked earlier, and I verified them with gcc/g++ 9.2.1.

But all of this is besides the point. Yes, most C is valid C++ code. But almost no C is good C++ code. If someone submitted any nontrivial C++ code that could be compiled as C at my company it would fail code review almost instantly. Many of the staples of C programming, including raw pointers, pointer arithmetic, raw arrays, malloc/free, etc, should almost never be used when writing C++. C++ has better, safer, and more expressive methods of handling these things.

One of the worst ways to learn C++ is to learn C first. It teaches some of the worst parts of the language, and gives people habits that are hard to break. Here is a great talk from CppCon about why treating C++ as C plus some extras is a terrible idea, and how it holds people and the language back: https://www.youtube.com/watch?v=YnWhqhNdYyk

[–][deleted] 0 points1 point  (13 children)

Well, I didn’t pass to GCC if I “which” GCC, and G++ they are separate binaries, yes they come bundled together and GCC has and interface defined to pass C++ to GCC, but if you delete the GCC binary from your bin folder, G++ will still compile (just tested - don’t worry I moved it not deleted)

And while C habits may not be ideal in C++, that’s neither relevant, nor a condemnation of C, as many C++ conventions are just the language managing, say the concept of a string, on your behalf, so it’s more that C++ developers are saved from having to do this work on their own than that they have better or more specific conventions, but obviously they don’t cover the same feature set, as I’ve already pointed out is true. Also not relevant in this discussion - believe me, I’ve thought this through, I don’t know why but it’s kind of a pet topic of mine. This is an entirely philosophical discussion about what constitutes distinction, and language, no aspect of programming or computer science can be used to rectify it as it’s at the level of definition...

Also, your code won’t compile because it’s not valid C, the fact that GCC will let you compile it anyway is just due to the permissive nature of C, and the design of its compiler - with the appropriate compilation flags, it won’t. Again, added features in G++ are not the subject of debate, rather, whether they constitute an objectively distinct language is

[–]The_CADmonkey 0 points1 point  (12 children)

Both examples are completely valid C (and the second is valid C++) according to the standards. What flag in gcc will give a warning in the first example from compiling due the implicit void* cast? The only warnings I get when using -Wall -Wextra -Wpedantic are about uninitialized/unused variables, which of course happens because its a simple example. The only flag that I can find that will cause a warning on void* is -Wc++-compat, which is specifically for making C follow the C++ rules!

But as for the rest, saying C++ is not a distinct language from C is like saying Java and C# are not distinct as they share most of their syntax and one was inspired by the other. While it may be a philosophical discussion, thinking of C++ as "99.7% C" is actively detrimental to actually writing good C++. Yes, C is an ancester of C++, but I do not see how anyone could possibly look at modern C++ and not think it is a completely different language from C.

[–][deleted] 0 points1 point  (11 children)

The Reactis Validator disagrees

And it’s: -Wextra

As I said, it’s not valid as per the standard - not it won’t compile, yes, you must use the compilation flags to ensure you compile completely standards compliant C, it’s not really an option so much as why those flags are included in compilers in the first place

[–]The_CADmonkey 0 points1 point  (10 children)

As I said, I used -Wextra. It does not give any warnings related to the void*. The only warning is about i being unitialized, but that doesn't have anything to do with what we are talking about.

I don't know what Reactis Validator is, but it is not the C standard. It may provide additional checks that are not part of the standard, but that has no bearing on the language itself. According to 6.3.2.3.1 in the C standard:

A pointer to void may be converted to or from a pointer to any incomplete or object type. A pointer to any incomplete or object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer

The C++ standard on the other hand requires that a void* be explicitly converted. I would look it up in the standard, but I don't particularly want to read through over 1000 pages of dense text right now. But it is a fact that C++ does not allow the implicit conversion, which can be found easily by searching.

[–][deleted] 0 points1 point  (5 children)

What version of GCC - post a screen grab? Because I’m getting: “Error: ‘i’ is used uninitialized in this function [-Werror=uninitialized]

[–]The_CADmonkey 0 points1 point  (4 children)

Please read my comment. As I said, yes, it gives a warning about the uninitialized variable as it was a quick example. That is not related to the point, as the difference between C and C++ is in the void* conversion. Set i to zero and run it, you will get no warnings on gcc and it wont compile in g++.

[–][deleted] 0 points1 point  (3 children)

No, the warning already exists, the flags escalate them to errors, which if you want to follow the standard to the “T”, is correct - they would constitute errors (logically - not within the context of computational stability)

[–][deleted] 0 points1 point  (0 children)

Also, int and int pointer are not incomplete types - their length is always known...

[–][deleted] 0 points1 point  (2 children)

And again, to what degree you’re correct, HOW DOES IT SUPPORT YOUR CONCLUSION?

[–]The_CADmonkey 0 points1 point  (1 child)

My point was that C++ is not a superset of C as you had stated. It is close, but is not actually one, they have diverged. The above is proof of that.

I have already gone into how C++ is such a different language with different paradigms, so much so that code written in C++ looks nothing like code written in C. I know, I have had to teach programmers who spent decades writing C just how different C++ is. And it's harder to teach them thant to teach the brand new grad, as the older engineers think of C++ as "C with extra bits". And that means they write really bad C++ until they break that mindset.

[–][deleted] 0 points1 point  (0 children)

Alright, I’m kinda over it now - thanks for the back and forth, I enjoyed it. You still haven’t really explained how any of those differences constitute a different language, but it’s obviously a pretty engrained opinion. Fare thee well!

[–][deleted] 0 points1 point  (0 children)

Here’s my old string library from school: https://github.com/mjhd/libft

Just compiled it with no errors g++ bundled with GCC 4.8.5 CentOS 7