Live data from Hacker News

Popular Myths about C++, Part 2

isocpp.org

61–70 of 93 posts

Re: Popular Myths about C++, Part 2

#61

I've heard so much laid at C++ feet over the years, but I don't find it all that horrible to work with. It seems about on the same level as Python to get most things done, just without all the magic of the python standard library. Are there some good stories of the bad of C++ that anyone can share?

Herb Sutter's Guru of the week is a nice collection of gotchas/best practices: http://herbsutter.com/gotw/

And of course, his books (More) Exceptional C++ (http://www.gotw.ca/publications/xc++.htm, http://www.gotw.ca/publications/mxc++.htm) also contain nice examples.

Re: Popular Myths about C++, Part 2

#62
post #36

Earlier quoted context omitted.

This leaks argument is really getting old. I have no leaks in my code. C++11's addition of move semantics and using references everywhere makes pointers unnecessary for the most part. You can use STL containers for putting your items into so shouldn't see raw "new" or "delete" operations in your own code very much; this is particularly true where you define your own move operators and move constructors. Even "old" C+…

Just wondering: why the downvotes?

HN Users have a constructor similar to this one.

  class HNUser
  {
  public:
    HNUser() {
      if (commentPraisesCorCPP()) Downvote();
    }
  };

Re: Popular Myths about C++, Part 2

#63

So far, all the comments seem to be missing Stroustrup's biggest point: garbage collection only works for memory. Destructors work for everything - memory, file handles, semaphores/locks, database connections, everything. Garbage collection handles 90% of the problem transparently to the programmer. That's wonderful (really - I'm not being sarcastic here). But the problem is, garbage collecting languages usually don'…

> You have to remember to close all your own files, clean up all your database connections, and so on. Garbage collection usually makes the 90% trivial, at the price of giving you no tool at all for dealing with the other 10% (non-memory portion) of resource management.

In languages that use lambdas/closures, you can make some sort of RAII as well.

It is a well known pattern in Lisp and ML languages.

Re: Popular Myths about C++, Part 2

#64
post #58

Earlier quoted context omitted.

My biggest complaint about C++ is all of the non-intuitive "gotchas" and corner cases hidden all over the place that seem to change with every new release. A lot of times the obvious code is either subtly broken and/or less efficient than one would expect. Reading through "Effective Modern C++," I was constantly thinking, "How is anybody supposed to remember all of these corner cases?"

I concur with this. I use C++ daily and have been for about 20 years, and I still run into gotchas and things I misunderstand all the time. I feel it's even worse than that, though. There seems to be a culture among C++ programmers to do things in overly clever ways and to push the limits of readability and understandability. Just look at the headers for the standard C++ template library, for example. (I'm currently…

You get to see this at CppCon 2014 videos.

On one side, very rigid companies barely using C++11, mostly as a better C with improved type checking.

On the other side, researchers making use of every template metaprogramming trick they can remember of.

Re: Popular Myths about C++, Part 2

#65
This article correctly observes that "many resources are not plain memory" and then goes on to advise users to close() a file in a destructor. However, one of the ways that files are not like memory is that while free() can never fail, fclose() can fail if writing is buffered and the final write fails. This is especially problematic in C++ since throwing from a destructor is dangerous. Whether you rely on RAII or GC to free files, you will not be able to catch this sort of error. It is precisely for this reason that GC is a mechanism for memory management, not resource management in general.

Re: Popular Myths about C++, Part 2

#66
post #30

Earlier quoted context omitted.

Another thing to think about: Program in C++ if you have memory to waste on leaks, or time to waste on plugging leaks. GC induces things that are arguably leaks, but in the average case they're controlled; leaks in C++ grow without bound in the average case.

This leaks argument is really getting old. I have no leaks in my code. C++11's addition of move semantics and using references everywhere makes pointers unnecessary for the most part. You can use STL containers for putting your items into so shouldn't see raw "new" or "delete" operations in your own code very much; this is particularly true where you define your own move operators and move constructors. Even "old" C+…

> This leaks argument is really getting old. I have no leaks in my code.

I imagine you are in the lucky position to have full control over the whole code, right?

Back on my C++ days at work, 1999 - 2005, there was always a lucky guy having to track down pointer misuses across the project source code.

Re: Popular Myths about C++, Part 2

#67
"My precious language isn't broken! It's not! Not! Not!" Strostrup writes something like this about once a year. There's some denial there.

The basic problem with C++ is that it's hard to tell if something really bad is happening. Think of C++ from the view of the maintenance programmer assigned to figure out why a C++ program is crashing intermittently. Or worse, the security expert trying to figure out how a system was penetrated. C++ has hiding ("abstraction") without safety. This is a bad combination, one seen in few other major languages.

We make progress in programming partly by having the language eliminate some problem. High-level languages mean you don't have to worry about register allocation, saving and restoring registers, or calling sequence conventions. Assembler programmers have to obsess on those issues. C++ doesn't eliminate any problems from C. It helps with some of them, but not to the point that they're just gone.

The three big questions in C are "How big is it?", "Who owns it?", and "Who locks it?". C++ helps a lot with the first one, even though it doesn't have enough subscript checking to guarantee the absence of buffer overflows.

C++ has struggled with the second one, with three rounds of auto_ptr, unique_ptr, and now reference-counted smart pointers. But because these are an afterthought, implemented using templates, they're neither optimized nor airtight. The mold keeps seeping through the wallpaper, in the form of raw pointers. Rust has a designed-in solution to this problem. (Rust's borrow checker is perhaps where C++ should have gone, but C++ will never get there.)

C++ as a language has no clue about who locks what. The language totally ignores concurrency. That's said to be an operating system problem. This is now a very dated concept.

(As I say occasionally, I really hope the Rust crowd doesn't screw up. They address all three of those big questions in effective ways. But I see too much use of "unsafe" in Rust code, which indicates weaknesses in the language design. The use of Rust's "unsafe" for "performance" is a big problem. From a semantic standpoint, only "Vec" needs "unsafe", because somebody has to convert raw memory to an array of objects. Everything else can be built on top of "Vec". But there is unsafe code for "performance" in hash classes and such.)

Re: Popular Myths about C++, Part 2

#68
post #30

Earlier quoted context omitted.

Another thing to think about: Program in C++ if you have memory to waste on leaks, or time to waste on plugging leaks. GC induces things that are arguably leaks, but in the average case they're controlled; leaks in C++ grow without bound in the average case.

This leaks argument is really getting old. I have no leaks in my code. C++11's addition of move semantics and using references everywhere makes pointers unnecessary for the most part. You can use STL containers for putting your items into so shouldn't see raw "new" or "delete" operations in your own code very much; this is particularly true where you define your own move operators and move constructors. Even "old" C+…

All: In HN comments, please don't complain about being downvoted. It's off-topic, explicitly against the site guidelines, and tedious.

Re: Popular Myths about C++, Part 2

#69
The problem is not what C++ can or cannot do. Or if you can avoid its issues (which are many), or even how long you need to be to get competent at it.

The problem is that you spend most of the time dealing with issues that are created by the language, not from the problem domain.

Every minute you are debating which kind of smart pointer to use, interpreting error messages (which are quite verbose), writing copy constructors and assignment operators, or forgetting to add a virtual destructor, is a minute not working on your problem domain.

If it is performance you're after, then C++ is also bad in the sense that it's very difficult to reason about. Due to optimizing compilers, that's true to C, but the abstraction is way thinner.

Re: Popular Myths about C++, Part 2

#70
post #37

I've heard so much laid at C++ feet over the years, but I don't find it all that horrible to work with. It seems about on the same level as Python to get most things done, just without all the magic of the python standard library. Are there some good stories of the bad of C++ that anyone can share?

My main gripe is the mental overhead when compared with other languages and the bulkiness of the syntax. Languages that contain lists and maps as first class types and the syntax to go with it are so much nicer. It's no joke that large C++ applications contain a poorly defined subset of common lisp. The standard library is small. Things that are included out of the box in other languages do not. Doing some simple thi…

Yeah, I totally agree on that point. With C++, it's not entirely uncommon to write lines like the following:

    std::vector exampleparser::tokenize(std::string some_input){
That is unless I'm doing it wrong.
Post reply on HN