News for hackers. Stuff that matters.
Re:The Dark Side of C++ (2007) by dkarl on Saturday May 14, 2011 @08:43PM (#2548985)
Again the operator overloading thing.

Yes, it's true. && might do something different than you might expect if you're a C programmer. If you're a C++ programmer, then you know somebody might have overloaded the && operator. Yes, they could do it in some weird and unexpected way. Just like somebody can declare a weird and unexpected macro in C.

Yes, people who define C++ classes you use can screw you with dangerous or incorrect behavior, like throwing exceptions from destructors. Just like people who write C libraries can screw you with dangerous or incorrect behavior.

auto_ptr is not useless. At least, I assume it's good for something. Who knows? If not, who cares? boost::shared_ptr is "poor man's garbage collection," not auto_ptr. If you cite the documented behavior of a type as a huge and unwelcome surprise, then gosh, I know exactly how to avoid surprises like that: read the freaking documentation before you use the type.

Oh, and again with the "I can't tell what this code does." Here's a crazy scenario in C: call a function. Do you know what the code does? No, you don't. How awful! Unless you look at the function you just called, or maybe just glance at its documentation.

Half of these criticisms of C++ boil down to, "It's okay for functions to have unknown behavior that I have to learn about before using them, because I'm used to that, but it's not okay for types to have any kind of unknown behavior, because I'm used to C, where that is not the case."

Re:The Dark Side of C++ (2007) by coliveira on Saturday May 14, 2011 @09:48PM (#2549086)
> It's okay for functions to have unknown behavior that I have to learn about before using them, because I'm used to that, but it's not okay for types to have any kind of unknown behavior, because I'm used to C,

It is not an issue of behavior but of understanding.

In C++, a large number of things can be happening in a single line: an operator being called, a type conversion, a template instantiation, an overriden function in one of a dozen of classes, a constructor called in some part of the hierarchy, in some namespace... who knows. You have to be smart enough to figure out what is going on in each of these cases. As you code base grows, this makes it increasingly hard to understand what is going on.

Re:The Dark Side of C++ (2007) by mattgreenrocks on Sunday May 15, 2011 @12:22AM (#2549300)
By that logic, you shouldn't use any dynamic language, because someone could monkey-patch every function and you'd have no guarantee what exactly was being called.

Also, you forgot macros in your list of gripes, as they can stomp on literally ANYTHING indiscriminately, despite being declared far away from the actual invocation point.

Re:The Dark Side of C++ (2007) by nightski on Sunday May 15, 2011 @03:45AM (#2549543)
+1 on dynamic languages, not a fan for this and many other reasons

Haskell seems to go a long way towards solving this problem in my humble opinion. Meaning it can have very dense code but as long as you follow the types it is often fairly straightforward to figure out what a piece of code does.

The worst part about all the things that can happen implicitly in C++ is that they can all have side effects! Oh fun. This is not just a pitfall of C++ either, Java/C# can have the same issue - one statement can trigger a whole plethora of side effects making a large brownfield development project one hell of a nightmare.

Re:The Dark Side of C++ (2007) by danieldk on Sunday May 15, 2011 @05:38AM (#2549661)
Haskell seems to go a long way towards solving this problem in my humble opinion. Meaning it can have very dense code but as long as you follow the types it is often fairly straightforward to figure out what a piece of code does.

As a Haskell fan, I have to say that there is a bag of hurt here as well: performance in the lazy evaluation regime. It can be very hard to predict/understand when thunks are evaluated and the effect it has on heap use.

I am still hoping for a strict, pure language with monads to go mainstream enough to use it.

The next Haskell will be strict - Simon Peyton Jones

Source: http://www.cs.nott.ac.uk/~gmh/appsem-slides/peytonjones.ppt

Re:The Dark Side of C++ (2007) by fleitz on Sunday May 15, 2011 @03:30AM (#2549525)
A friend of mine actually encountered #define FALSE 1 in some header file he included from a 3rd party project. It was straight C.

I'm not sure why after finding that he decided to file a bug report rather that not touch any of that code.

Re:The Dark Side of C++ (2007) by asomiv on Sunday May 15, 2011 @06:21AM (#2549704)
#define FALSE 1 is not that uncommon in C because C in its infinite wisdom doesn't have true/false literals so people have to invent those by themselves.

Glib goes into great lengths to avoid namespace conflicts so it defines the GBoolean type and G_TRUE/G_FALSE. Whether that really is better than #define TRUE/FALSE, I don't know.

Re:The Dark Side of C++ (2007) by tmp43522 on Sunday May 15, 2011 @06:46AM (#2549731)
#define FALSE 0 is the common one
Re:The Dark Side of C++ (2007) by asomiv on Monday May 16, 2011 @03:41AM (#2551926)
Doh, you're right. I had '#define FALSE 0' in my mind. '#define FALSE 1' actually is insane.
Re:The Dark Side of C++ (2007) by exDM69 on Monday May 16, 2011 @06:39AM (#2552143)
In C99 #include <stdbool.h> has the type bool and the constants true and false (even in the "freestanding" flavor). Of course this doesn't save you if you are dealing with old/non-conformant compilers and/or legacy codebases.
Re:The Dark Side of C++ (2007) by kklimonda on Sunday May 15, 2011 @11:42AM (#2550114)
It actually defines TRUE and FALSE, and not G_TRUE/G_FALSE. But it does indeed define the gboolean type - it does make code easier to read.
Re:The Dark Side of C++ (2007) by burgerbrain on Sunday May 15, 2011 @11:49AM (#2550127)
People don't have to invent them, and it's generally annoying as hell when they chose to.
Re:The Dark Side of C++ (2007) by asomiv on Monday May 16, 2011 @03:40AM (#2551925)
So then what are they supposed to do, require a library X which has already invented them, while your app is using library Y which already does the same thing as X?
Re:The Dark Side of C++ (2007) by burgerbrain on Monday May 16, 2011 @06:10PM (#2554391)
Use 0 and 1, like everyone else. Those #defines are just meaningless syntactical sugar that get in the way.
Re:The Dark Side of C++ (2007) by coliveira on Monday May 16, 2011 @11:21AM (#2552795)
It is true that the pre-processor can create a lot of confusion, however it is easy to generate a -E version of the file that shows all substitutions. It is not even close to the confusion created by templates, for example.
Re:The Dark Side of C++ (2007) by dzorz on Saturday May 14, 2011 @09:37PM (#2549067)
Actually you can't overload && although you can overload &.
Re:The Dark Side of C++ (2007) by beagle3 on Sunday May 15, 2011 @04:21AM (#2549590)
You can overload &&. You just can't make it short circuit like the default is.
Re:The Dark Side of C++ (2007) by dzorz on Sunday May 15, 2011 @06:59AM (#2549744)
Wow, that is amazing. I honestly never tried that and assumed (because of short circuiting) that it can't be overloaded.