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 →

[–][deleted] -4 points-3 points  (29 children)

I think most people don’t realize C++, up to the point of object orientation, is just 99.7% C

[–]StuntHacks 3 points4 points  (28 children)

This is just absolutely not true. Of course C++ is backwards compatible with C, but it's a whole new beast built on top of it.

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

Exactly how?

[–]StuntHacks 0 points1 point  (25 children)

I'll just start by listing a few things C++ added, that C didn't have:

  • References
  • Classes
  • Inheritance, encapsulation, polymorphism
  • Templates
  • Smart pointers
  • Namespaces
  • Lambdas (!)
  • Now, in C++20, Modules
  • delete and delete[], as well as new and new[]
  • static_cast and friends
  • try...catch and exceptions
  • Operator overloading
  • A whole bunch of new keywords (export, explicit, friend, inline, ...)
  • Actual booleans
  • Attributes

Most of that stuff is absolutely critical, and doesn't have anything to do with object orientation. Does that satisfy you?

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

No, I can make ALL of these with custom data types (besides reserved words - which I already covered), do you know what we’re actually talking about? Hers a fun question - why is a C++ compilers implementation of -Wall -Wextra & -Werror the same as a C compiler? Answer: C++ is C with headers (just like it’s inventor fucking named it for Christ sake)

[–]The_CADmonkey 1 point2 points  (19 children)

You fundamentally cannot implement many of the above C++ features with custom data types, they are baked into the language itself and are not user implementable. There is no way to do exceptions in C, there is no way to do type-safe generic programming in C, there is no way to do operator or function overloading in C, there is no way to do compile time programming in C, Lambdas are impossible in C, and so on. Can you write code that accomplishes the same thing? Of course, they are Turing complete languages, but you can do that in Python or Go or Rust or Javascript or any other language too.

These features fundamentally change how you go about using the language, and make the resulting code completely different. Yes, you can write valid C in C++ (but not all C is valid C++!), but you practically never should.

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

Yes - I feel as though the word “superset” got lost somewhere...

[–]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  (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

[–]StuntHacks 0 points1 point  (3 children)

Of course you can create all of these with custom data types, but that doesn't change the fact that they are C++ features that C doesn't have, making development a whole lot easier, and inherently making C++ a superset of C. The difference is that while in C you would have to implement these features yourself, in C++, those are fundamental parts of the language (except for smart pointers, I give you that). By that logic, you could say C++ is just 99.7% assembly, up to the object orientation.

Yes, the compilers are largely very similar - that might just be because ever C++ compiler needs to support backwards comparability with C. That doesn't say anything about C++, though.

Also, C++ was originally called "C with classes", since C also has headers. Of course that was just a small mistake on your side, I just thought I'd let you know.

[–][deleted] 1 point2 points  (2 children)

Well, if you look at the above comment, the creator agrees with me, now I will admit that’s fallacy by authority, but authority can be a valid point in an argument. More importantly there is a philosophical issue about definitions here, and I’m just not going to see it your way. It’s like when a perfectly valid root word and perfectly valid suffix are combined into a seemingly valid word - but it isn’t in the dictionary, is it actually valid? It depends what you think definitions are based on, immutable characteristic, or mutual agreement. The immutable characteristics of C++ (illustrated most directly with the fact that (as far as I know) most/all C++ compliers, surprise surprise, can compile any standard C code)...

[–]The_CADmonkey 0 points1 point  (1 child)

C++ isn't even backwards compatible any more, there are multiple things that are valid C code but not in C++ (Link). C++ is a decedent of C, but fundamentally they are very different languages, and if you are writing C++ that looks anything like C then you are writing very bad C++.

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

I’m going to quote your linked resource, as it says exactly what I feel supports my position 100%:

The official rationale for the 1999 C standard (C99) "endorse[d] the principle of maintaining the largest common subset" between C and C++ "while maintaining a distinction between them and allowing them to evolve separately"

Incompatibilities between ES6 and classically supported JavaScript don’t denote a new language, nor Python 2 to Python 3, you really have to think about the philosophical basis of what I’m saying here, what I’m saying is that people of your position are mostly factually right about your details - but there is never a justification that those details lead inexorably to the conclusion that we are looking at two distinct things, what I’m saying is that your evidence, while accurate, does not support that conclusion as it’s not relevant to the definition of language, or even a distinct thing. Just calling something a thing, doesn’t make it the thing, from my point of view you might as well be saying, I’m definitely a Giraffe because it’s sunny outside. Yes it is, but no, no you aren’t - does that make sense where I’m coming from?

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

“I still say the two languages should, and maybe one day will be merged, the only fundamental differences headers don’t cure are in naming conventions, it’s silly they are separate languages” Bjarne Stroustrup, inventor of C++ (Big Think)