Jump to page: 1 2 3
Thread overview
[OT] C vs C++
Aug 26, 2022
Dukc
Aug 27, 2022
zjh
Aug 29, 2022
Dukc
Aug 30, 2022
zjh
Aug 30, 2022
Paulo Pinto
Aug 31, 2022
Paulo Pinto
Aug 30, 2022
Abdulhaq
Aug 30, 2022
Atila Neves
Sep 01, 2022
Dukc
Sep 01, 2022
Paulo Pinto
Sep 01, 2022
Dukc
Sep 08, 2022
Atila Neves
Sep 08, 2022
Dukc
Sep 08, 2022
H. S. Teoh
Sep 09, 2022
Dukc
Sep 09, 2022
H. S. Teoh
Sep 10, 2022
JG
Sep 10, 2022
H. S. Teoh
Sep 10, 2022
JG
Sep 13, 2022
H. S. Teoh
Aug 30, 2022
Ali Çehreli
Aug 30, 2022
JN
Aug 30, 2022
rikki cattermole
Aug 31, 2022
Paulo Pinto
Aug 31, 2022
Paulo Pinto
Aug 30, 2022
MrJay
Jun 23, 2023
drug007
August 26, 2022

C++ is meant as an advancement of C. And it's used as one. Dmd was written in C++ until version 2.069, and countless other programs, both open and closed source still use it. It makes sense - it's a superset of C (well, almost), so one can always fall back to C features when the more complex features of C++ don't justify themselves.

Yet, many of the most well-known and successful programmers don't see it like that. Can it be that C++ is so complex that even conservative use of it makes your codebase so unreadable that even the archaic C is a better choice? Think how crazy this is - the cream of of our profession resort to pointer / length pairs over std::vector, and copy-pasting the module name to every public declaration over using namespaces.

There has to be HUGE downsides in C++ for this competent people to resort to this drastic avoidance. They do say what the downsides of C++ are about: too big a language to learn well, so code ends up using features the reader does not know. Still, if this is the case one would think it had been long since generally aknowledged: C++ guidebooks would tell to avoid less-known language features absent strong reasons, and later languages ought to have more pressure to be more minimalist like Go and less "CISC" like D or Rust. Yet, complex D features like ranges (okay, more of a Phobos feature), operator overloading, CTFE, objects and templates don't seem to be commonly hated.

This inconsistency in our attitude towards language complexity is interesting in my opinion. I want to hear your opinions, would you rather use C or C++ in your job if you had to pick one ("it depends"-answers okay). But most importantly, why? What do you make of that C++ complexity seems to be so appreciated and so at contempt at the same time?

August 27, 2022

On Friday, 26 August 2022 at 21:16:20 UTC, Dukc wrote:

>

C++ is meant as an advancement of C.

The choice between C++ 'and' C is very simple. Because C++ can be more abstract.

In my eyes, the advantages of the latest C++: templates, concepts, variable template parameters, folding expressions (very convenient), , and corotine etc. There are also various easy-to-use containers.

What d needed is to reasonably arrange personnel and determine the order of the problems to be solved.

Problem is complex, so 'C++' is complex. Similarly, when it needs to be complex, it must be complex.

August 29, 2022

On Saturday, 27 August 2022 at 01:22:31 UTC, zjh wrote:

>

On Friday, 26 August 2022 at 21:16:20 UTC, Dukc wrote:

>

C++ is meant as an advancement of C.

The choice between C++ 'and' C is very simple. Because C++ can be more abstract.

In my eyes, the advantages of the latest C++: templates, concepts, variable template parameters, folding expressions (very convenient), , and corotine etc. There are also various easy-to-use containers.

That's roughly what I'd think too. Still, so much excellent programmers pick C over C++, at least according to the article I linked.

Why they do that, that's what I'm wondering. I can maybe understand it for some firmware or device driver where everything is so low-level anyway that C++ doesn't offer much benefit but otherwise it's just mind-boggling.

>

What d needed is to reasonably arrange personnel and determine the order of the problems to be solved.

D wipes the floor with both of them anyway.

August 30, 2022

On Monday, 29 August 2022 at 19:15:41 UTC, Dukc wrote:

>

On Saturday, 27 August 2022 at 01:22:31 UTC, zjh wrote:

>

but otherwise it's just mind-boggling.

Nothing. They just chose the wrong language at the beginning. When they found out, they could not jump out.
They have accumulated their own function library, they can not do without it.

August 29, 2022

On 8/29/22 3:15 PM, Dukc wrote:

>

That's roughly what I'd think too. Still, so much excellent programmers pick C over C++, at least according to the article I linked.

Keep in mind, that article is 13 years old. C++ has changed a lot since then. So has the programming language landscape. D2 was at version 2.035.

-Steve

August 30, 2022

On Monday, 29 August 2022 at 19:15:41 UTC, Dukc wrote:

>

On Saturday, 27 August 2022 at 01:22:31 UTC, zjh wrote:

>

[...]

That's roughly what I'd think too. Still, so much excellent programmers pick C over C++, at least according to the article I linked.

Why they do that, that's what I'm wondering. I can maybe understand it for some firmware or device driver where everything is so low-level anyway that C++ doesn't offer much benefit but otherwise it's just mind-boggling.

>

[...]

D wipes the floor with both of them anyway.

Only if you ignore the ecosystem, and that the two best optimizing D compilers have a backend written in C++, better grammar and semantics only gets you so far.

August 30, 2022

On Friday, 26 August 2022 at 21:16:20 UTC, Dukc wrote:

>

This inconsistency in our attitude towards language complexity is interesting in my opinion. I want to hear your opinions, would you rather use C or C++ in your job if you had to pick one ("it depends"-answers okay). But most importantly, why? What do you make of that C++ complexity seems to be so appreciated and so at contempt at the same time?

I'm a polyglot developer (going back to assembler, LISP, even COBOL for a few weeks), but most of my professional coding has been in Python, Java, and older 4GLs (LOL). However I have used C and C++ a lot in my hobby code and I've learnt over time that I now understand them better than many 'professionals'.

Around 20 yrs ago I decided to write a chess playing program that would make moves based on a strategy rather than brute force. I first wrote it in Java (around 1.4.2) and surprise, surprise it was dog slow. So I rewrote it in C++ and I was very disappointed to find that it was still very slow, in nodes per second, compared to other engines. Finally I rewrote it in C, and saw good performance. I later realised this was because in Java and C++ I was using 'new' for each computed new position (node). In C I just made a huge flat array up front at the start.

Anyway, to learn C++ I had used some sort of Teach Yourself C++ in 24hrs book (yes, really). It taught all about the wonders of inheritance etc.

Later, I started reading Herb Sutter's Guru of The Week column. I decided that there were too many gotchas and reluctantly abandoned C++ for hobby work. I also read a book with interviews with many famous software engineers, and most of them hated C++ (https://www.goodreads.com/book/show/6713575-coders-at-work).

At this stage I was helping to engineer high level designs for commercial aircraft using Qt, VTK, SciPy, python, CAD kernel software - we were doing good, technical work to a high standard. It was professionally held together with good quality python code calling libraries written in C++ and Fortran.

Onwards, and I read Andrei's TDPL. I was excited by this! My first foray into D was exciting but ultimately I had to write too many tools to build a tool to build a tool. I also did not see a clear direction for D, and decided to sit it out (where I am today).

Then, I read Bjarne's A Tour Of C++, which is excellent. I practised with it and interviewed for a couple of C++ jobs. I realised that they knew less about C++ than me, and were living in the world of C++ 98. Also, the pay was not very good! I did not pursue those jobs.

Finally, now to answer your question, I would choose to use C++ over C if I was coding in a small team. For a bigger team? I might well just sit it out and keep looking for work in python/java.

August 30, 2022

On Friday, 26 August 2022 at 21:16:20 UTC, Dukc wrote:

>

C++ is meant as an advancement of C. And it's used as one. Dmd was written in C++ until version 2.069, and countless other programs, both open and closed source still use it. It makes sense - it's a superset of C (well, almost), so one can always fall back to C features when the more complex features of C++ don't justify themselves.

[...]

I'd much rather use C++ over C. As to why C programmers don't migrate, well:

https://www.youtube.com/watch?v=D7Sd8A6_fYU

August 30, 2022

On Monday, 29 August 2022 at 19:15:41 UTC, Dukc wrote:

>

Why they do that, that's what I'm wondering. I can maybe understand it for some firmware or device driver where everything is so low-level anyway that C++ doesn't offer much benefit but otherwise it's just mind-boggling.

  • C is the only language that can be considered as a lingua franca and will remain in that position in the forseeable future. Far more programmers can use your library if you write it in C than in any other language.

  • C++ does not have an OS mandated ABI, which most of the time is an advantage as you can evolve and optimize more, but not for all use cases.

  • You also loose a lot if you use C++ without a runtime. As a consequence of that it is much easier to convert a random C library so that it works without a runtime than to do the same with a random C++ library. So if you want your library to be useful in all contexts then you are better off writing it in C.

  • You can easily convince yourself that the C-library you write for Python does not export symbols that Python does not require. It is easier to understand the consequences of using language features in C when writing plugins etc for other languages.

  • C allows more typing hacks, more things are undefined behaviour in C++.

  • "restrict" is standardized in C, compiler dependent in C++.

Does it make sense to pick C over C++ for language reasons? No… usually not. Are there other reasons? Yes.

August 30, 2022
On 8/26/22 14:16, Dukc wrote:

> one can always fall back to C features when the
> more complex features of C++ don't justify themselves.

It can be too easy to fall back to C without justification. Real code I've seen not too long ago:

   foo(("hello" + bar()).c_str());

Why the c_str() there I wonder... But don't worry: foo makes a fresh string to put it in a struct object member anyway. O... K..., I guess...

Another one:

  strncpy(obj.path, zar("blah").c_str(), FILEPATH_LENGTH - 1);

Four problems:

1) As mentioned, what the c_str()!

2) Faith-based programming by using obj.path's hard-coded array size as FILEPATH_LENGTH.

3) Where is the null terminator, buddy? Rather, when are you going to read the man page?

4) What happens if the path doesn't fit?

Isn't that way too many problems in a single line of C++ code?

> Can it be that C++ is so complex

True.

> that even conservative use of it makes
> your codebase so unreadable that even the archaic C is a better choice?

I disagree. I had decided long time ago that C++ would always beat C by the following features alone:

1) Constructors and destructors

2) Exceptions

3) Templates

Except, you need to code to a platform where a C++ compiler is not available.

I say that under long experience with a very well-written C framework. It was trying to be C++ and that's fair but C is too cumbersome to use. Defining equivalents of vector, hash map, etc. with macros is amazing but gets painful very quickly.

I liked the bail macros though; they were imitating exceptions:

  err = foo();
  bair_error();

finally:
  // Do cleanup
  return err;

But correct usage of those goodies depended on programmer attention. For example, you had to have an 'err' variable in such functions, you had to 'return err', etc.

> There has to be HUGE downsides in C++ for this competent people to
> resort to this drastic avoidance.

Let's not underestimate humans. It is much easier to rationalize staying with your favorite language than learning a new one. And C++ makes that choice easy.

Personally, I went to laughable extents to search for dislikable things in Rust. :/

> it had been long since generally aknowledged: C++ guidebooks would
> tell to avoid less-known language features absent strong reasons

There are the clearly failed ones like std::auto_ptr, std::strstream, etc. Other than that, C++ people carry the esoteric guidelines like badges of honor. I used to be one. I was proud that you could not write good C++ without having read 5 specific books. And it was an equity issue as well: Those books were not available to most of the non-English speaking people.

C++ is a language that requires one to read and learn and read and learn and follow at least 400+ rules to write correct programs. There is no other way to say it. And that is nothing but a failure. (Why am I bashing C++ again! Argh!) My respect towards leaders of the C++ community fell because they don't see any problem and continue to sell C++ as the best thing ever, "inventing" everything, etc. They even say things like "Another language is not a solution [...] Java tried..." Stomping language design just because Java failed is not an argument. How about getting back to what C++ tried and failed. And instead of saying things like "A language with reference types? No thanks!", C++ experts should say "A language with 400+ rules to follow? No thanks!"

> This inconsistency in our attitude towards language complexity is
> interesting in my opinion.

One word: Humans...

> would you
> rather use C or C++ in your job if you had to pick one

Hands down C++. And again, I used both of those languages very extensively in very good written frameworks.

> What do you make of
> that C++ complexity seems to be so appreciated and so at contempt at the
> same time?

Humans... I did ask a Google intern once who was visiting from Europe and working on some awesome C++ project. He was an expert in C++ and was presenting his work at one of our local meetups. I asked him "Why C++ when there are so many new modern languages popping up?". He laughed and said "Because it's hard!".

C++ is carried forward by an amazing mass of very smart people who are in it for the standardese, the conferences, knowledge of the esoteric ("do you know how you can create integer ids at compile time for somethings in your struct definitions and blah"), blog posts, etc.

Ali

« First   ‹ Prev
1 2 3