No wonder you find C++ pleasurable, you are using it as C with classes! However, if you are working on an existing code base with mix of STL, boost and bunch of other libraries, you would start feeling pain of using C++. In my experience, every library author uses a different subset of C++ and as an application writer, you often have to grapple with the glaringly different idoms in your program. I find boost pleasurable to use; however looking at boost source is enough to melt my mind.
Almost everywhere C++ (language and libraries) try to satisfy every corner case and sacrifice usability of common case. e. g. STL iterators (at least I have BOOST_FOREACH to soften them). Ah well this is probably contributing very little to the discussion but I feel better now :) So let me redirect to [C++ FQA](http://yosefk.com/c++fqa/) and let yourself decide whether the points in FQA make sense to you.
I also find C++ pleasurable (90% of the time) and in my code, I make heavy use of templates (including compile-time calculation/preparation of data, ie not only simple "templates as generics"), exceptions (well, not heavy use, I use them for handling exceptional circumstances, nothing more), STL (especially the containers), std::string (C strings and custom string classes are evil), Boost (not so much, but it has some great libraries), Qt (GUI mainly, but also the support libraries) and Intel Threading Building Blocks. I do not use multiple inheritance, but I certainly use classes and single inheritance, pointers, smart pointers and memory pools.
But I still like C++.
I agree that it has its problems and it certainly isn't a good language for beginners [1], but it is a very flexible and powerful language that is useful for a range of tasks from low-level to high-level code (though I guess it excells at mid-level: higher level that C, but low level enough for performance sensitive code, eg games). I certainly wouldn't recommend it for a lot of tasks, but I wouldn't avoid it either.
I'm not a big fan of the FQA, but the FAQ Lite is very good: http://www.parashift.com/c++-faq-lite/index.html also, Bjarne Stroustrups FAQ is excellent: http://www2.research.att.com/~bs/bs_faq2.html
[1] Even experts got this quiz wrong: https://scapecode.com/2009/10/a-simple-c-quiz/
We could reasonably replace C++ with a combination of C and a higher-level language. However, C++ is a much better language for our low-level code than C, and we have a lot of low-level code, so we have to take it seriously. If we took the route of using a high-level language to coordinate functionality implemented in C, we would end up using C++ to implement the "C" libraries, just for the benefits of RAII, exceptions, smart pointers, etc.
We could also succeed with a stable and efficient Common Lisp implementation. Other languages such as OCaml might work for us, but I'm not qualified to say.
We wouldn't bother changing at this point, though, unless we decided to create an entirely new product to compete with our current one. C++ is not a problem or a limitation for us. The shortcomings of C++ add a little bit of extra work, but they aren't a multiplier. Most other languages would aggravate our existing problems instead of just adding a constant factor of inconvenience.
And that's with templates, exceptions, STL, and Boost!
I have found C++ to be too complex to fit within my head. Even though I am programming almost exclusively in it for a while; I get a feeling that I am spending more time on placating the compiler than thinking about code.
For cross platform work that uses a lot of libraries I would not recommend C++. It is where my frustration stems from.
I think most good C++ programmers spend years coming up to speed. I know I spent many hours coding for fun in college and read through TC++PL a couple of times (also for fun) before I started programming in C++ professionally. It was a wise investment, no doubt about it. It's an open question whether it would be a wise investment today, though.
https://github.com/scalien/scaliendb/tree/master/src/System/...
Yes, we try to avoid op.overloading, exceptions, and we explicitly don't use the STL, Boost, <any library>. All the code is ours, which is really nice if you're debugging, finding performance problems, etc. It was our experience on previous projects that pulling in outside libraries in a systems level project like a database ends up wasting time (spent in understanding, debugging and optimizing outside code) over just writing what you need. This has proven to be a good call, we fix all bugs, however deep or nasty very quickly.
Thanks for the UnitTest++ tip.
{ embedded programmer }
If you are going to complain about a self assignment check in the assignment operator function, maybe you should stay away from any kind of programming at all.
And I don't understand this complaining about which subset of C++ needs to be used. I have written scala code and it most definitely does not use every feature of scala.
The scenarios in which any of the C++ features need to be used is fairly obvious.
1. STL - use it all the time, it is one of the core libraries of modern C++ 2. templates/overloading - You will use these features when you use STL. You typically won't create you own template classes. 3. exceptions - This is one of the C++ features used less often. Dealing with it is no different from dealing with exceptions in other languages like Java.
> I find boost pleasurable to use; however looking at boost source is enough to melt my mind.
The idea is that you use boost source and not look at it. boost libraries are a very specialized kind of template meta-programming code that only container writers have to deal with. I like being able to use a smart_ptr without having to worry about silly memory leaks, none of which would have been possible without templates or operator overloading in plain vanilla C.
There are several deficiencies with C++, that I experience on a day to day basis, none of which are discussed in any of these online C++ rants. I have never faced any software engineering issues because of the multitude of C++ features. That has never been the problem. If anything all these features reduce the amount of boiler plate that I need to write to get my job done.
You may not like STL iterators, but they are a 1000 times better than some of the gnarly C macros I have seen in C code to get STL like containers in C. Once you have seen that kind of C code, you will be glad about the existence of templates.
EDIT: downvoters: if you think I'm trolling, please note that I've written more code in C++ than any other language. I would love a "better C" that's universally better than C++. If you disagree with my points, please elaborate.
In the Microsoft ecosystem, C# is this. No, C# isn't OS-independent or hardware-independent or HTTP-server independent. It's not meant to be. C# isn't meant to be an abstractly beautiful language like Lisp or highly performant and portable like raw C. And it's not intended to be part of the LAMP free-software stack; it's intended for a business context that can spend a bit of money to make money. (The C# compiler is actually free, though the IDE isn't and of course the underlying Microsoft OS isn't.)
It's C and most of C++ without the foot-shootiness or Java verbosity. For what it is meant to be and for the problem domains that it serves, C# really does make for great productivity. My company and I have used it as our primary language for several years and I like it a lot. Most of the productivity comes from tight integration to external ecosystem elements that make for a useful application, notably MS SQL databases and ASP.NET web servers, all wrapped within a solid IDE. More productivity comes from getting to ignore things like memory allocation and header files which the language just handles for you. Perfect for simply getting things done in a business context when things like price and portability are not a concern. (When they ARE a concern, then by all means use the LAMP stack and C# is not for you.)
Calling C# or Java a "better C" is therefore not terribly useful. C is not good at the things C# is good at and vice versa. I use about 6 or 7 different languages on a regular basis, and often the only choice is between C and C++.
I'm not sure where you're going with the second paragraph. C and C++ were never popular for general web app development.
You've been brainwashed by Microsoft. No one using LAMP cares about price (that much) or portability ('L' is for Linux). Freedom from vendor lock-in (and in this case, a predatory, vengeful vendor) is the biggest practical advantage. Stability and the thing actually fucking working is another (I never enjoyed babysitting Windows servers; do you?). It's also easier to build robust applications with LAMP.
Enjoy paying your Microsoft tithe, peasant.
D is doomed by the Phobos/Tango schism, no-one would risk their business on it.
The Objective-C additions just aren't very low-level. They're basically an object runtime library with some syntactic sugar. This is fine for situations where you want something a bit more high level and dynamic[1], but gives you no advantage over C in something like device drivers or programming embedded devices, because you just aren't going to use those features. Minimalistic (optional!) vtable dynamic dispatch is great for these uses, though, and generic programming can be really nice (despite C++'s version in the shape of templates) even for low-level applications. RAII also is really nice.
D is doomed by the Phobos/Tango schism, no-one would risk their business on it.
If D was that much better, I think a sufficient open-source ecosystem would spring up around at least one of the two libraries that you could realistically use it. As it is, I doubt it would be popular even without the schism.
[1] Though in my opinion, most iOS apps would be easier to write in a managed language, especially if you could call out to C/C++. The Obj-C focus is understandable from Apple's point of view though.
Maybe, but just as you can not easily dismiss C++ because so many people have written so many successful programs in it, it is difficult to dismiss all of the great software from Apple, and arguably the largest library of software for mobile devices in the App Store, written in Objective C.
You point out the resource constraints that make C++ superior to Objective C and other languages for drivers and embedded systems. Similarly, I suspect Objective C gives Apple the right trade offs for writing sophisticated software for mobile devices that is responsive, uses memory effectively, and does not kill battery life.
C99 is such a contender imho.
C99 is a pretty tiny (but welcome) incremental improvement on C. It's also almost entirely orthogonal to C++'s additions. Calling C99 a C++ replacement is pushing it, so you'll need to elaborate on why you think so. I'd be curious about an instance of something you can do better (let alone fundamentally better) in C99 than C++. I can think of plenty of examples of the opposite.
C++ sometimes makes me want to tear my hair out, sure[1], but when using pure C(99), I find myself thinking, "this code would be so much more compact in C++."
[1] This is usually because either I or someone else tried to be too clever. Otherwise it's because of something C++ does badly that neither C89 nor C99 do at all.
I didn't call it a C++ replacement. Just a C89 replacement. "A better C" :)
> I'd be curious about an instance of something you can do better (let alone fundamentally better) in C99 than C++.
It depends. If you need C++'s syntactic sugar for OOP-style code then yes - C++ is better for that. But if you don't do OO-heavy stuff you can use plain C and get away with less complexity (and more control over the instruction cache, etc).
I'm in the lucky position of not needing OO so I get away with using C. And C99 makes my life, compared to the old C89 standard (and the C++ C subset), easier. I can declare variables in the middle of a function (or even within the if statement!). I can initialize structs by its member's names thanks to designated initializers. And the implicit "upcast" from void* to whatever is a blessing (though possible in C89 it isn't available in C++). Oh, and I almost forgot variable length arrays.
> but when using pure C(99), I find myself thinking, "this code would be so much more compact in C++."
Well, this can go the other way round, too. Just take the right tool for the job.
There's no language currently out there that covers as much ground as C++ and is anywhere near as good at it. C99 falls into a similar bucket as Go or Objective-C in covering a subset of cases where C++ is useful.
I echo that sentiment. I'm using C++ in my current project (first time in many, many moons), and it dies what we want reasonably well, but it is bigger than any language needs to be.
Have you tried OCaml? It's has it's problems, but it's nowhere near as bad as C++.
I've never tried to find C++ programmers on the market, but I do seem to come accross fewer and fewer C++ programmers now. I use a lot of C++ myself (amongst other languages, including Clojure, Java, Python and recently SML). I think having learnt it long before becoming a professional programmer may have given me a kind of attachment to it, but I actually enjoy using it (most of the time, I do often wish it did things differently or supported features I see in languages like ML or Lisp derivitives).
As for your interview question, the first seems almost trivial. The second, an intrusive stack, isn't very hard either [1]. I'm surprised that most applicants have trouble with them!
[1] Heres one I slapped together in ~5 minutes (no comments, only tested by running that code, etc etc): https://gist.github.com/827619
A game developer disusses why he often writes his own data structures and steps out of STL and gives concrete examples at http://altdevblogaday.org/2011/02/15/data-structures-one-siz...
(fwiw I don't have a dog in this discussion. I don't use C++ and never will. The above is offered only because I had that page open in another tab when I read your question, and it seemed relevant)
Personally, I will write my own data structures, containers or memory allocators when I have a task that doesn't fit nicely into existing libraries. There is nothing wrong with that. I am curious though when people only use their own. For example, I believe that when writing custom containers, providing STL compatible interfaces (perhaps alongside a non-compatible extension if needed) is a good idea because then you can use the STL when it makes sense, but your own when it doesn't (and pass data between them easily).
Having said that, I did not spend long looking at the code in question (nd in the sibling comment a good reason was presented for at least some of the custom containers), so perhaps it isn't realistic or reasonable to mix and match in this case. Still, I like to understand peoples reasoning because it helps me improve my own coding practices.
The basic issue is that we didn't want to include external libraries, because it's a pain to debug, pain to build on multiple platforms and there are licensing issues.
Also we don't use so many datastructures, so we rolled our own and we are happy with them. For example we have an intrusive treemap, that has a function for returning the middle of a sorted key which is useful when you want to split a keyspace into two. Not sure if any of STL/Boost supports that.
Your solutions looks good, you are hired! ;) Seriously, we know that it is not that hard, but still the applicants have problems with it.
Sure, obviously for niche tasks a general library may not perform as well as a container custom-made for the task at hand. That certainly is a legitimate reason to roll your own (though, if it were me, I would make my containers STL compatible, similarly to how Boost is STL compatible, that way you can mix and match between standard containers and custom ones and easily convert between them (eg, by using compatible iterators)).
Regarding applicants, I am just as surprised as you are. I guess I didn't notice how much C++ is diminishing in industry (outside of its niche areas, anyway).
PS: You're database does look interesting. May have to keep an eye on it.
The issue is picking the particular subset and then getting everyone to adhere to it. That works okay in mature engineering shops such as google:
http://google-styleguide.googlecode.com/svn/trunk/cppguide.x...
So I guess I'd say it requires more discipline than, say, Java. (One could similarly argue that writing good Perl requires more discipline that writing good Python.)
There's also John Carmack's quip:
IMO, good C++ code is better than good C code, but bad C++ can be much, much worse than bad C code.
Different folks internalize different features of C++, for good or bad. To take someone's comfort zone and institutionalize it is arrogant.
There's nothing wrong with that; having that stuff in your style guide means that you get more consistent code, which is easier for everyone to read whatever their personal preferences.
(General point: Having a rule that says "Everyone must do X rather than Y" doesn't mean claiming, or thinking, that X is better than Y. There's not much to choose between driving on the left side of the road and driving on the right, but it sure is valuable to choose one or the other and stick with it.)
It's interesting that C++ is increasingly becoming a niche skill. When I started programming you pretty much had a choice of C++ or VB if you wanted a job.
Suits me fine though. A lot of the most interesting work (DSP, kernels, graphics etc) is in C++.
http://stackoverflow.com/questions/5004162/what-does-it-mean...
Are you sure applicants aren't failing because the application problem for the intrusive stack is written in a confusing manner?
I tried editing the post, but edit functionality doesn't appear to be working correctly.
I don't know, its very possible. I found the application problem very easy to understand and writing an intrusive stack is really easy, so unless I'm very disconnected from the "average applicant", who apparently 1) isn't very good at C++ and 2) doesn't know how to implement common data structures, the problem must be with the application.
I love programming in a sensible subset of C++ too.
The problem is that C++ has long history and when it began, many programmers had erroneous ideas about OO - some skipped it completely, some built absurdly deep inheritance trees, some engaged in ridiculous tricks to stay within "doctrine" (remember a fellow who had a pointer-member to the same object in a parent and a child class just avoid something about down-casting).
So from this history, there's a lot of really bad C++ code "out there".
And just much, when you take two code bases or two groups of programs, each happy with their different sensible subset of C++ and combine them, without being careful, you also wind-up with a hellish mess.
What is an instrusive stack?
If you need to do systems level programming (eg. write a database), and you want to go beyond C's code organization capabilities, and you can select a sensible subset of C++, it's a great language, and you get to control all aspects of memory and CPU.
We're writing our database(s) in C++, and we're pretty much loving every minute of it, and would not switch to C, Java, Erlang or whatever.
Of course we're a startup, so we got to select our own subset, write our own containers, etc:
http://github.com/scalien/scaliendb
One downside is it's hard to find good C/C++ programmers on the market. The vast majority of applicants cannot complete our first interview filter (removing a characters from a C string and writing an instrusive stack in C++):
http://scalien.com/pdf/job.pdf