Someone on Medium Just Said C++ Was Better Than C (medium.com)
Developer David Timothy Strauss is publishing a call to code "straightforward, easy-to-reason-about approaches" -- in an essay titled "Choosing 'Some C++' Over C". (Alternate title: "C++ for Lovers of C."
The problem with just picking C++ is that most criticism of it is legitimate. Whether it's the '90s-era obsession with object orientation and exceptions or the template errors that take up an entire terminal window, there have been -- and remain -- rough edges to C++. But, these rough edges are avoidable, unlike the problems in C that get worse with modern event and library programming.
The opinionated essay calls for "adopting a subset of C++ to smooth out C's rough edges," arguing that C++ offer a better, type-safe approach for event-driven design (as well as destructors to avoid memory allocation leaks). Are there any readers who'd like to weigh in on the advantages of C versus C++?
"C++" > "C" (as long as > has not been overloaded..)
Slashdot, fix the reply notifications... You won't get away with it...
Code in assembly, then you can come back to my lawn..
Sig ?
At some point, the developer community will wake up to just how evil C -syntax- is, and how much it has contributed to bugs and security holes.
enough sed. don't bash me.
If you post it, they will read.
Seems like his gripe is actually with STL and possibly curl's interface, not C++ per se.
I like C++ (classes, exceptions etc) but generally avoid using STL except possibly for basic things like cout, strings and vectors. STL code becomes unreadable FAST, and quickly turns a simple problem into a giant pain in the ass, especially when debugging.
Set the handle to send a pointer to the struct. The handle expects a void *, so there’s an implicit cast from the struct’s pointer type to, effectively, nothing.
A void pointer is NOT a pointer to nothing. At least it better not be. :-)
"Transparent" is a shit show that trades on every stereotype going. A man in drag is NOT a transsexual.
Not quite, it is in fact undefined.
C++ is the post-increment operator, it increments the variable, but then returns the original value. Therefore, since C started out as 0x11, C++ will evaluate to 0x11 while modifying C to be 0x12 as a aside effect.
Therefore, if you were > optimistic you could try to claim that "C++ < C", expecting the operations to be evaluated left-to-right and thus be evaluated as "0x11 < 0x12". However, C++ doesn't specify the evaluation order of operators, which means that "C" might end up being evaluated before "C++", in which case the comparison would be evaluated as "0x11 < 0x11" instead. The only thing you can be sure of is that C++ will NOT be greater than C.
Basically, as a rule of thumb you should never modify a variable within a line of code if the value of that variable will matter anywhere else within that same line. http://en.cppreference.com/w/c... - discusses undefined behavior halfway down the page.
--- Most topics have many sides worth arguing, allow me to take one opposite you.
People who know and like C will continue to use it. Giant legacy projects written in C (like the Linux kernel) will continue to use C, of course. C++ 11, IMO, is not going to convince C programmers to switch. Rather, it's a love letter to existing C++ programmers due to the radical way it transformed the language.
It's still ugly as hell, and has sharp corners that will slice your fingers and toes off if you're not careful (we ARE talking C++ here). Even so, for the first time, using just the core language and libraries (Boost doesn't count), I can completely avoid manual memory management in 99% of the cases, almost like using a garbage collector, but with the advantages of destructors for non-memory resources. Raw pointers are almost a thing of the past, except for some very rare exceptions, and move semantics means your APIs can look like you always wanted them to while still remaining efficient. It's hard to describe how different the language feels pre and post 11.
In my opinion, C++ 11 simply makes C++ far better at creating large, high-performance libraries and applications (in my case, videogames) because of the confidence that robust ref-counted resource management gives you.
Irony: Agile development has too much intertia to be abandoned now.
C++11 and C++14 added some nifty utility features which make the language worth considering as a replacement for C, even if you have no use for writing your own classes or complex templates (the latter being the usual reason why people would use C++).
I've been seeing this C++ vs C comparison for quite a while now, especially on Slashdot. The usual argument is "C++ can do everything that C can, and more, hence it is better than C".
It's a damn stupid argument to make, and the makers should rightfully feel stupid for making it. Say "C++ can do everything C can, and more" is just a different way of saying "C++ has all the gotchas of C, AND MORE".
We write the code to be read by humans, hence anything that adds to the errors made while reading is a *bad* thing, not a good thing. It's fine you add new cognitive traps as long as you are removing existing ones!
When I need a language that is higher level than C, I do not reach for C++ - the alternatives are better. When I don't need anything better than C I use C.
Frankly, I cannot think of a situation in which the lack of features in C cause me to reach for C++. When I want those features I'd reach for Java instead and code the performance critical stuff in C anyway.
The repeated comparisons showing C++ to be a better C is stupid because C++ has all the traps that C has, and adds a whole lot more. When I need the features offered by C++ I can use Java[1] and avoid the pitfalls of C *and* C++.
[1] Depending on circumstances, I'd use Java for large-team dev, Lisp when I'm solving problems solo (hobbying), Tcl+Tk or Python+Tk when I'm doing a once-off make-my-life-simpler GUI script. Mostly I use bash. For daily dev as an embedded guy in safety-critical environments I use C.
I probably won't change to C++ for this case, as having the ability to look at a code snippet in isolation and know exactly what assembly will be generated is an advantage.
I'm a minority race. Save your vitriol for white people.
It's my most familiar language, back from when I was learning it on the schoolbus by reading K&R. I would still never choose C over a carefully-selected subset of C++ for a new project. There is just no advantage to keeping things more primitive except when it comes to very specific environments, like traditional Unix kernels. I think templates are very useful in limited doses and far superior than macros, inheritance is somewhat useful to almost any kind of CS problem, and the STL itself is a huge boon to software reliability and interoperability.
Of course, I also have no qualms with Java, so....
Brian Fundakowski Feldman
In the 1990s I worked on a complex device driver for OS/2 for ATM networking (asynchronous transfer mode). The driver was around 100K lines of C++ code however only a subset of C++ was used. Surprisingly the use of C++ worked out quite well. We had an equivalent driver for Windows NT that was written in C that was over 300K lines of code. The C++ codebase was a lot more reliable and easier to work on, despite all the work that was done to make C++ work in kernel mode under OS/2. The C++ driver actually had more functionality than the C driver and it was faster as well with a smaller binary. Also, as I said, it was a subset of C++, especially in the performance critical code. The driver in question included the entire ATM signalling stack and implemented full LAN emulation support with both Ethernet and Tokenring plus it could emulate multiple networks (ELANs, equivalent to VLANs) simultaneously. When I implemented multiple ELAN support I was afraid it was going to be a nightmare, but due to the way it was architected in C++ I ended up only having to change a few lines of code, changing a pointer to the LANE class to an array of pointers.
For the signalling stack and ILMI C++ worked out especially well due to the event and message based nature of it and the various state machines. For LAN emulation it made it easy to support both Ethernet and Tokenring by having a few virtual functions in the main LANE class.
There was no exception handling support and none of the more complex C++ features were not used. It used templates but that was also somewhat limited.
Having destructors was also quite nice, making it easy to clean up resources.
Despite being C++, debugging wasn't too bad though at the time the OS/2 kernel debugger basically ran at the assembly level.
If the infrastructure is in place I can certainly see using C++ in the kernel and device drivers.
This post is encrypted twice with ROT-13. Documenting or attempting to crack this encryption is illegal.
I found the same thing when I was handed a large project in C++. In this case it was a very complex kernel-level OS/2 device driver. It took me a while to adjust to C++ but once I did I certainly saw the advantages.
With C we're always reinventing the wheel and doing things the hard way. I say this as someone who works on bootloaders and has worked on a lot of device drivers and works close to the hardware. C++ requires more support than C to get basic functionality working, but once it's there a lot of code becomes simpler.
This post is encrypted twice with ROT-13. Documenting or attempting to crack this encryption is illegal.
I have been writing C since the early 80's and C++ since the early 90's. For large-scale robust systems, C++ is the way to go. It provides for greater degrees of abstraction, debug-ability, and clarity of intent. I only use C any longer for kernel development.
Sometimes, real fast is almost as good as real-time.
In exchange for manual memory allocation, C++ gives you automatic memory allocation: lots of it.
Nonsense. You don't get memory allocation unless you ask for it.
When resources are scarce (eg. IOT devices) this overhead can be a show stopper.
You're misinformed. With C++11 move semantics, you can have both safe, automatic ownership management, and no unnecessary dynamic allocation. Most of my work is done in a very constrained environment, where I have only a handful of pages of heap... or in some cases none at all. C++ is awesome for such environments.
Something that C++ advocates seem to ignore there is no free lunch.
No one is claiming that there is. What there is, is the opportunity to delegate tedious and error-prone due diligence that C programmers have to do themselves to the compiler. For example, you know all those functions that have comments describing whether the returned data structure's contents are owned by the caller or the library? In C++ you can write the function so that it's impossible for the caller to avoid taking ownership when that's what you want, or so that it's impossible for the caller to believe it has ownership when the library is retaining it. If the caller gets it wrong, the compiler will flag the error. That's one example, there are many, many more. C++ enables you to have buffers and strings that do automatic bounds checking... or even to write code such that potential bounds violations are flagged by the compiler, making run-times bounds checks provably unnecessary.
There's no magic here, just language constructs that allow you to accurately specify the semantics you want, which the compiler can enforce.
Note to ACs: I usually delete AC replies without reading them. If you want to talk to me, log in.
C++ is much better than C. It's much greater expressiveness makes it easy to clearly formulate what you are doing, and in far fewer lines of code too. Exceptions free you from all that tedious boilerplate, where every function call basically expands into three lines: error=function();if (error) handle_error (error);. RAII makes resource handling painless. It's massively more powerful standard library provide instant access to lots of useful datastructures and algorithms, and unlike C it's all typesafe too.
Is it hard to use? Hardly. I find C hard to use - just imagine having to write an application that uses strings, it'll be one giant mass of mallocs, strcats, strcpys, frees (don't forget any!), and will invariably end in buffer overflows and lost memory. Oh, and it will probably have a whole bunch of gotos for what they laughingly call 'resource management', Dijkstra's 1968 paper notwithstanding.
Do I disagree with all the criticism, then? No - but the horror stories that get posted here do tend to be worst possible cases, which pop up once in a very long while, rather than the daily occurrences some people make them out to be. It's been... I don't know, half a decade or so? since I last saw one of those horrifying template errors - and it's not for lack of templates in my code. It's not really a hard language either - sure, you _can_ write unreadable statements, but you can do that in any language so that doesn't mean much. It also gives you the tools to write much, much clearer code.
I always roll my eyes when people mention needing a 'cut-down C++'. That's lack of understanding, usually mixed with a liberal dose of unwarranted fear, and better advise would be "use common sense". For example, there is nothing wrong with overloading operators, but common sense indicates one should not change the meaning of those operators. Having your own number-like class is fine (for example, for complex numbers, bignums, money, whatever), and overloading operators for it is an excellent idea. Using operator+ to paint a widget or retrieve data from a database - maybe not so much.
So, yeah, C++ is an amazing language. Hmm, that makes me wonder if there will be an article on Medium now, revealing that someone on Slashdot just said that. I don't know that website, maybe they are not into clickbait so much...
C++ meets or beats C in all but a very few respects. Designated initializers is a really painful and stupidly short sighted omission. C++ has a wealth, even a surplus (see "most vexing parse") of initialization mechanisms, but none of them can do what design initializers do. Unfortunately, when converting C code to C++, this frequently results in maintainability regresses as code has to be converted to rely more on positional parameters... which you hope you have correct, but you don't always, and you hope the order of members never change, but it does.
When all you have is a hammer, every problem starts to look like a thumb.
I first started using C++ back in the 1980's.
I am a huge fan of classes. When C++ was basically a preprocessor for C which introduced the class keyword, I thought it was pretty cool.
When exceptions were introduced to the language I thought C++ was fairly complete as a language. If from there the designers of C++ had addressed the fragile base class problem, lambdas and some form of introspection, I think C++ would have been a fantastic language.
Instead, we got templates. I'm not a fan of templates.
And when we got the standard template library, I thought someone was smoking crack. Using the left shift and right shift operators to mean "input" and "output"? Really? Really?!?
When I write C++ I try to stick to using a subset of C++ which includes classes and exceptions. I use templates sparingly, only when they are really needed, and I refuse to use the C++ left-shift and right-shift operators. (I really feel like the person who designed that thought "how cool; we can override a shift operator to mean input and output!" But just because you can doesn't mean you should, and now we're stuck with this bit of syntactic bit of bullshit.
We're finally getting around to lambdas, though too late: Apple has already wedged blocks into a non-standard extension. And I'm not holding my breath on introspection or on fixing the fragile base class problem simply because the run-time implementation recommendation for classes way back in the late 1980's has become a baked in de-facto feature. (Sadly it would have been relatively easy to solve by introducing link-time assignment of the indexes in the virtual table pointer; that way, as the base class is recompiled the index references for the methods in the virtual table could be reassigned at link time. This also solves fragile access to public class fields; simply replace them with a standard getter and setter method which is accessed via the virtual table.)
C++ done right is do vastly different from C that debating which language is better is beyond silly. This goes threefold if you look at C++14 programmed with the GSL - the right way to do C++ these days.
In a nutshell C is assembler 2.0 and C++ is assembler 3.0. C++ has massive inner api advantages over C that C tries to compensate for with libs such as boost.
Yet build with C++ without knowing what you are doing and of course you'll produce bad software. With C you simply won't get anywhere.
Wether you use one or the other these days is often a matter of personal preference more than anything else. C++ has massive ready-made power with the responsibilty that comes with it. Any programmer looking at these PLs will see the difference and adapt his style of coding accordingly.
We suffer more in our imagination than in reality. - Seneca