5 ms·
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 c
by jandrewrogers 13y ago
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.
- cmbaus 13y agoPersonally 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.
- mpyne 13y agoC++ 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.
- cmbaus 13y agoI 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 http://accu.org/index.php/articles/1308
- mpyne 13y agoThat'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.
- cmbaus 13y agoI'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.
- coliveira 13y agoOk, 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.
- maxlybbert 13y agoC++ 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/ 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.
- digikata 13y agoFor 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++.
- fhd2 13y ago> 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.
- mpyne 13y ago> 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.
- dustingetz 13y agoDatomic, 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.
- barking 13y agoThis was a pleasure to read
- AceJohnny2 13y agoFollowing that argument, C allows as much control, with fewer gotchas.
- mpyne 13y agoMore "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.
- codex 13y agoAnd 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.
- comex 13y agoAs 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.
- pux0r3 13y agoMy 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)
- cageface 13y agoThe 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.
- thebear 13y agoAuthor 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.
- cageface 13y agoI 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.
- kgabis 13y agoActualy 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/technology-briefs/intel-labs-closing-ninja-gap-paper.pdf http://www.intel.com/content/dam/www/public/us/en/documents/...