:: Re: [DNG] C vs C++
Etusivu
Poista viesti
Vastaa
Lähettäjä: Didier Kryn
Päiväys:  
Vastaanottaja: dng
Aihe: Re: [DNG] C vs C++
Le 02/10/2024 à 01:12, nick a écrit :
> Yes, the languages have diverged. The other day I had to port a small
> test program from C to C++, as there was a radio driver written for
> Arduino that I wanted to evaluate before potentially translating it to
> C for my project. In my test program I was doing this:
> struct request request = {
>   .channel = ...;
>   .command = ...;
>   ...
> };



    You are using "designated assignment", which a good feature taken
from other languages and introduced in C89 IIRC.

> And the C++ compiler kept rejecting it. I was quite puzzled until I
> remembered that C++ initializes structures in order, this is quite
> fundamental to the language to allow it to clean up if an exception is
> thrown during the initialization. So a different syntax is used. And
> the C99 (?) syntax can't just be bolted on, even though the committees
> both try to harmonize the languages as far as possible.
>
> C++ has some nice features that C lacks, such as std::vector, std::map
> (and STL and templates generally), vtables (that is to say,
> polymorphism and virtual functions), namespaces. These are features
> that I often have to code manually in C. And I think a C with these
> features could be useful. So the concept is good in general. Yet after
> working with C++ for some time I'm not enthusiastic.
>
> There is a fairly restricted use case for C++ which is when you need a
> very high level language due to the complexity of your project but you
> absolutely cannot tolerate any slowdown due to abstraction. This was
> the case in my research group as a postgrad as we were writing
> constraint solvers that were heavily mathematical but needed to search
> a combinatorial solution space which could take hours even for small
> problems. Google also uses C++ for similar niches such as the V8
> javascript engine and possibly some of their products like search or
> maps which must serve billions of requests daily.
>
> Apart from that, please avoid C++, and if you must use it, make class
> hierarchies very shallow and avoid overloading, templates, template
> base classes, virtual base classes, mixins and multiple inheritance.
> When these features are overused the result is a horrible spaghetti
> where the logic of your code is smashed into a million bits sprinkled
> through your source files in such a way that it is impossible to see
> what lines of code execute and in what order. Oh the frustration!!!
>
> I worked with such codebases and it drove me mad. My uni recruited a
> well known researcher who had written a popular engine in C++, but
> after having worked with this engine I had grave reservations about
> the project. When he arrived and started work I could see he planned
> to recreate the same mess on our behalf. As politely as I could I
> begged him to reconsider his approach but he pooh poohed my concerns.
> A few years later his project had evolved into another horrible
> spaghetti and also had a lot of bugs. I attempted to do some code
> cleanup and submitted bug fixes but he refused to accept them saying
> "it's not a bug it's a feature!" ... sigh. Really avoid C++ please
> please please.