News for hackers. Stuff that matters.
Re:C++ and the Culture of Complexity by jandrewrogers on Friday August 23, 2013 @06:39PM (#6266629)
A lot of the nominal complexity of C++ is the result of the very precise control it offers over software behavior. In many programming languages this level of control is not even expressible. That extra level of control only seems like unnecessary complexity until you are designing software systems that require this level of control. At a minimum, precise control of memory management is absolutely essential for things like servers and database engines if performance matters at all.

That the author thinks C++ is competing with Go betrays a narrow perspective. There are many important things you can do in C++ that are not possible in Go, in part because of the features that make C++ "complex" that simpler languages do not have. As an obvious example, the renaissance of C++ for high-performance server systems is based in part on the fact that garbage collectors are a major drag on performance for these kinds of codes.

Which is not to say that C++ is not excessively complex, just that the author seems to lack an understanding of why some of that complexity is essential functionality.

Re:C++ and the Culture of Complexity by cageface on Friday August 23, 2013 @07:58PM (#6266926)
The author points out the extra complexity that C++'s programmable value semantics have added to the language without also mentioning the important benefits.

First, you can create a lot of objects on the stack, which avoids any dynamic allocation at all and is far, far faster.

Second, you can precisely control the layout in memory of your data structures. For example, you can represent a set of vertices in an OpenGL scene as a contiguous array of structs. This gives you much, much better cache performance and also makes it easy to hand data directly off to hardware components like graphics cards that need input in memory formatted in specific ways. In languages like Java you have to resort to awkward, inefficient hacks like FloatBuffers to do the same thing.

Re:C++ and the Culture of Complexity by kgabis on Saturday August 24, 2013 @04:39AM (#6268111)
Actualy it might be better (cache-wise) to declare a struct of arrays (SOA) rather than an array of structs (AOS).[1]

[1] http://www.intel.com/content/dam/www/public/us/en/documents/...

Re:C++ and the Culture of Complexity by thebear on Friday August 23, 2013 @11:15PM (#6267449)
Author of blog post here: It's true, I haven't done a good job of mentioning the benefits that you get in return for the complexity of C++, especially as far as value semantics is concerned. Thanks for setting that right.
Re:C++ and the Culture of Complexity by cageface on Saturday August 24, 2013 @02:57AM (#6267944)
I haven't done much C# programming but it looks to me from studying language tutorials that you can have the best of both worlds in many cases in C#. You can have value objects and densely packed arrays of structs if you need them but Java-esque reference semantics everywhere else.
Re:C++ and the Culture of Complexity by cmbaus on Friday August 23, 2013 @07:16PM (#6266766)
Personally I think the notion that C++ offers "precise control" as you call it is overrated. For instance flexible memory allocation is touted as an advantage, but in practice this is limited by the fact that there is only one global implementation of new the operator.

Over the long term I think Go is going to offer a much more sane environment for implementing high performance servers.

Re:C++ and the Culture of Complexity by mpyne on Friday August 23, 2013 @07:21PM (#6266782)
C++ does allow overriding the new operator, actually.

It also allows for running just the constructor in an area of already-allocated memory, if you're particularly crazy.

With that said it's hard to argue that Go makes concurrency easier at this point, which can certainly make it more useful for many of the types of problems programmers are seeing today.

Re:C++ and the Culture of Complexity by maxlybbert on Friday August 23, 2013 @08:10PM (#6266964)
C++ allows both overriding (i.e., replacing) and overloading (i.e., creating multiple versions of) operator new. When overriding, you still have to call some memory allocation routine; and you should also override operator delete, and should probably override operator new[]/operator delete[]. There are some pitfalls in doing this on some platforms ( http://pavlovdotnet.wordpress.com/2008/01/12/jemalloc-builds... ).

The tricky part, to me at least, is when overloading. It's possible to overload new in a way that "new int" calls the original new, and "new int, foo" calls your overloaded allocator (foo could be an object used for the sole purpose of knowing which new to call, or it could actually be used by that new -- maybe it's a memory pool). That's easy enough to do. The surprise comes from the fact that while you can overload operator delete, it's a real pain to call the overloaded version (plus, of course, you have to remember which memory came from which version of new). Assuming you use "foo" to specify your overloaded new and delete, "delete p, foo" is a syntax error. You need to call something like "::operator delete(p, foo)". At least that was the case with C++03; I don't know if C++11 changed anything. Personally, I just overload new, but call a destroy() function instead of an overloaded delete.

Re:C++ and the Culture of Complexity by cmbaus on Friday August 23, 2013 @07:32PM (#6266821)
I realize this, but you can't provide multiple global implementations of new.

It is difficult to implement multiple allocation strategies in the same application. In another time, I actually did quite a bit of work in this area: http://accu.org/index.php/articles/1308

Re:C++ and the Culture of Complexity by pcwalton on Friday August 23, 2013 @07:48PM (#6266878)
You can just provide an allocate() static method and destroy() method on your object that use placement new and call the destructor manually. Or don't and just use placement new/manual destructor calling yourself.

Are you trying to argue that C++ programs can't use custom allocators because of language restrictions? That's demonstrably false; Gecko and WebKit, just to name a couple of C++ codebases I'm relatively familiar with, use custom allocators everywhere.

Re:C++ and the Culture of Complexity by cmbaus on Friday August 23, 2013 @07:57PM (#6266922)
Fair enough. My point was poorly made, and truthfully it has been along time since I've done significant C++ work. But it is difficult for the users of libraries like the STL to mix and match their allocation strategies, so for all practical purposes, most applications end up using the default implementation, or a single override of global new.
Re:C++ and the Culture of Complexity by coliveira on Friday August 23, 2013 @09:20PM (#6267167)
Actually if you may create a policy for a particular type by implementing operator new at the class level. I think you're not well informed, because C++ is the language that gives you the most flexibility to design memory management schemes.
Re:C++ and the Culture of Complexity by mpyne on Friday August 23, 2013 @07:47PM (#6266876)
That's true, but I'm not sure what it would even mean to have multiple implementations of the same function at the same time.

Do you mean something like operator overloading where a given type has a different implementation of new that is used for it? Or a way to select different implementations of the global operator new at runtime? It seems to me that it should be possible for the latter to make it switchable, although the code would be inelegant to say the least.

AFAIK the former is possible as well, but the problem is that I think the type-specific operator new/delete have to be members of the type which means that you can't add it on later.

That's a great article btw.

Re:C++ and the Culture of Complexity by cmbaus on Friday August 23, 2013 @08:45PM (#6267072)
I'm going to dig deep here, and say that true flexibility in memory allocation would require multiple global implementations of new, and when an object is deleted it would be destroyed with an implementation of delete the corresponds with the implementation of new.

That would be a gigantic change to the language, which I'm sure I don't understand the implications of.

After thinking about this some more, I'm going to stand by my original, even if poorly argued, comment that C++ doesn't really live up to the promise of "precise control" when it comes to memory allocation, and this is one area that can have a big effect on performance. C++ has all the overhead of a low level language, with a promise of control that when you really need it is often difficult to access.

Re:C++ and the Culture of Complexity by coliveira on Friday August 23, 2013 @09:34PM (#6267196)
Ok, so let's count... Just from the top of my head, we have global new/delete overloading. At the class level you have new/delete overriding. Then you have allocators that can be used with any STL container. In you templates you can also have custom allocators. Then you have new with placement syntax, which means that you can allocate memory with anything, including something like malloc or gmalloc. Of course, you can also use the stack for automatic memory... You can even use a garbage collector such as Boehm. And you really want to argue that there are no options for memory allocation in C++?...

The problem with C++ is exactly the opposite, it has too many options, which makes it difficult to create a global memory management policy, unless you define you own complete framework. Quite the opposite of what you said.

Re:C++ and the Culture of Complexity by comex on Saturday August 24, 2013 @02:10AM (#6267843)
I'm not sure whether the previous poster was thinking along these lines, but in some sense C++ makes it harder to control allocations compared to C due to a combination of increased abstraction and a general assumption that objects are fixed size. For example, if a C struct contains an array whose size is known at the time of allocation, there's a good chance the elements will be located after the end of the struct rather than being in a separate allocation. Of course, you could do this in C++, but neither language nor library provides sugar, so it ends up looking unnecessary compared to just using a std::vector.

I won't opine on whether it actually is unnecessary.

Re:C++ and the Culture of Complexity by mpyne on Saturday August 24, 2013 @07:38PM (#6270252)
Well at a language level C++ shares more or less the same behavior as C. If you input the same "plain old data" struct into C++, with an array whose size is known at compile-time, it would be placed where C places it.

Now you're correct that the standard library provided with C++ does not quite give you this same level of control. If you want node elements placed somewhere specific you either have to pick the right container, write a custom allocator (including rebind support so that any required container-internal elements also can use your allocator), etc.

But this is a complaint about a C++-only feature that C doesn't even provide.

On the very few times that C does provide an standard library function that will automatically allocate storage from the heap, it is even less customizable than the C++ equivalent. Normally your only option is something allocated with malloc(3), you're told to use free(3) when appropriate, and if you need something different you run into trying to replace malloc/free and friends and then somehow telling your custom malloc when to use custom behavior and when to use stdlib behavior. But you could do this in C++ code too.

Now you can certainly easily write your own library with better support for custom allocation/deallocation. Things like the Apache Portable Runtime, for instance, or glib.

But once you've bought into the idea of simply ignoring the language standard library anyways, you've bought into something else that you could do in C++ if you really felt like it were useful.

The problem with C++ is that the standard library makes it almost straightforward to do what you want and still be able to use standard containers without having to write your own, but we have to remember that we're talking about a problem that essentially can't even be spoken about in C. So it's not so much that C "doesn't have a problem", as that this problem couldn't even be expressed in C.

Re:C++ and the Culture of Complexity by fhd2 on Friday August 23, 2013 @07:50PM (#6266894)
> Personally I think the notion that C++ offers "precise control" as you call it is overrated. For instance flexiblememory allocation is touted as an advantage, but in practice this is limited by the fact that there is only one global implementation of new the operator.

I don't quite follow, who's stopping you from using sbrk(), mmap() and all the other low level allocation system calls if you really have to? AFAIK, many high performance applications have their own allocation mechanism. Furthermore, new/malloc is worlds apart from GC when it comes to precise memory management.

Now, to be fair, I have no experience implementing high performance servers. But speaking for games, GC gets in the way a lot. You can make it work by essentially avoiding allocation altogether, at least on systems where you can use tons of memory, but that's just a very crude workaround.

Re:C++ and the Culture of Complexity by mpyne on Friday August 23, 2013 @10:52PM (#6267381)
> I don't quite follow, who's stopping you from using sbrk(), mmap() and all the other low level allocation system calls if you really have to?

From a pure language perspective, nothing.

From a standard library perspective... it's probably more difficult. I won't say impossible, as the STL allocator concept certainly seems general enough to allow this, but definitely more difficult.

Re:C++ and the Culture of Complexity by digikata on Friday August 23, 2013 @07:29PM (#6266808)
For more flexible memory alloc in C++, can't one use placement new? That makes destruction more complicated, but maybe that's further support for the thought that complexity in C++ gives one more possibilities for precise control of the computing environment. How often one needs or wants to actually exercise that level control is another question.

At any rate, Go certainly does offer some nice tradeoffs vs C++.

Re:C++ and the Culture of Complexity by AceJohnny2 on Friday August 23, 2013 @06:59PM (#6266709)
Following that argument, C allows as much control, with fewer gotchas.
Re:C++ and the Culture of Complexity by codex on Friday August 23, 2013 @07:05PM (#6266732)
And assembly language even more control than C, with even fewer gotchas.

However, C++ occupies a sweet spot whereby it allows a lot of control, but also a lot of abstraction and structure--much more so than C, in my opinion.

Or, put another way: one doesn't build a skyscraper out of playdough or cinderblock (easy to use materials), but one doesn't mill the entire skyscraper out of a solid block of steel, either (that would be unjustifiably difficult). C++ is the language of skyscrapers.

Re:C++ and the Culture of Complexity by mpyne on Friday August 23, 2013 @07:18PM (#6266771)
More "gotchas" actually, from the secure coding perspective.

C is nicer in that its ABI is so simple that it's practically the lingua franca of inter-language linkage, but the fact that pointer types convert automatically to other pointer types is a huge source of errors, and so is the lack of language support for things like destructors.

Additionally there are many C++ concepts that cannot be implemented in C without the use of macros everywhere, such as using a sorting algorithm (which in C requires either a pointer indirection for a function call, or use of macros to generate a new sort function for each appropriate type).

Additionally in C++ you can use things like type expressions to select and compile the optimal version of a matrix-vector multiplication automatically, and I'm not even sure you could do that in C with macros as it involves programmatically generating types at compile-time.

C is certainly nice for some problems but your comment applies as much to going from C to asm as it does going from C++ to C.

Re:C++ and the Culture of Complexity by comex on Friday August 23, 2013 @07:09PM (#6266744)
As a data point, I would like to see a language much more like C than like C++ (including, somewhat but not entirely idiosyncratically for me, no classes), but with templates and some other improvements.
Re:C++ and the Culture of Complexity by pux0r3 on Friday August 23, 2013 @07:14PM (#6266757)
My ideal language is C but with C++ templates, better type safety (no implicit casting to void*), and with a smarter "include" system (ie: something that isn't just copy/paste one source file into another. Although it would be nice to be able to keep #include as well)
Re:C++ and the Culture of Complexity by bnolsen on Friday August 23, 2013 @07:52PM (#6266897)
i suspect something like "clay" would float your boat but I haven't followed it for more than a year now.
Re:C++ and the Culture of Complexity by dustingetz on Friday August 23, 2013 @07:19PM (#6266775)
Datomic, an acid database, has comparable features and more scalable performance compared to SQL based C++ databases. Datomic is written in Clojure, a functional programming language (the highest class of high level languages) and runs on the JVM.

In 2013, not a lot of problems require that level of control.

Re:C++ and the Culture of Complexity by barking on Friday August 23, 2013 @06:58PM (#6266701)
This was a pleasure to read