11 ms·
C++11 FQA anyone?
- inglor 12y agoThis is a very interesting read but it's full of FUD. You'd expect `==` to work in PHP because it compares value. With a char* reference the syntax and everything around it indicates that you're working with well... pointers (and you shouldn't be having char pointers for strings anyway). Don't even mention PHP references etc. C++ still has a lot of going for it - no one thinks it's terribly great and awesome - but the reason people ridicule PHP and admire C++ is that it solves a much bigger problem. - In PHP you don't have resource management, the process kind of just dies and resources get deallocated. In C++ that's not acceptable for perfomance. - In PHP you have memory safety - oh wait you really don't, if you do silly things like use raw references you can still get screwed.. experienced C++ programmers don't fall to that pit either. - In PHP you don't have to explicitly deal with memory which is again because you're solving a much easier problem. When your whole world is an immutable page request response cycle life is rainbows and sunshine. Comparing languages is always ridiculous and populistic but that comparison was just bonkers. Not that C++ doesn't have quirks - it sure does, but comparing it to PHP is just silly. (Not saying anything about PHP being a bad or good language here, just that the comparison is ridiculous)
- inglor 12y agoAlso, PHP doesn't give you type safety. I have no idea why the article said that but that's just ridiculous.
- banachtarski 12y agoComparing PHP with C++ is just silly in the first place. Nobody is trying to seriously write a deferred renderer that can do global illumination in PHP.
- saucetenuto 12y agoThe PHP runtime does know the types of all values at all times, though, in a way that the C++ runtime, insofar as it can be said to have one, does not. To call that type _safety_ is a little much even by the standards of dynamic languages, because PHP is so willing to do implicit type coercion, but _at runtime_ a PHP program is certainly more able to detect type errors than a C++ program.
- deleted 12y ago[deleted]
- astrange 12y ago> - In PHP you don't have resource management, the process kind of just dies and resources get deallocated. We have long running PHP processes for months at a time (hey, code reuse) and they do not suffer memory leaks. Works great actually! However, for desktop applications and short-lived processes, freeing app-level resources is pointless and only bad for performance. Why bother running through a destructor chain when you can keep your external data clean and just kill -9 the process? This is how iOS makes you work anyway.
- maxlybbert 12y ago> Why bother running through a destructor chain ... The Gold linker (written in C++) doesn't ( http://www.airs.com/blog/archives/362 http://www.airs.com/blog/archives/362 ): "[I]n the gold linker I often deliberately omitted destructors, because many of the data structures live for the life the program; in such a case, destructors serve only to slow down program exit." However, there are some things that you really want to know happened when the process exits for any reason. If possible, you'd probably like your files closed in a way that allows them to be re-opened. That's easy to do with destructors. Destructors: they were always for more than just memory management.
- astrange 12y ago> If possible, you'd probably like your files closed in a way that allows them to be re-opened. That's easy to do with destructors. That doesn't help with data corruption coming from the environment. The power going off on your machine, your external drive, a crash in your process or an external signal all won't give you time to exit. There are libraries worth relying on for this, like SQLite. It's not in C++.
- maxlybbert 12y agoThis is a weird argument: "I just bought a Porsche, and it handles better than any car I've owned!"/"you can't tow things with a Porsche." Anyhow, as you implicitly acknowledge, destructors help with orderly shutdown, even if the shutdown is caused by an error condition. I prefer them to try...finally that other languages rely on. I can live with C#'s IDisposable, but I personally have a better chance of getting my destructors right than implementing IDisposable correctly (remember, in a garbage collected language, you don't have a defined order that things will be destroyed in). But, on the completely different topic of how to do disaster recovery when both your program and the host operating system are shut down in a non-orderly manner: I personally am not aware of any language features in any language that make that easier. However, I know it can be done in C++ (stasis ( http://stasis.io/ http://stasis.io/ ), SQL Server, MySQL/MariaDB, Firebird ( http://firebirdsql.org/ http://firebirdsql.org/ ) and SciDB ( http://www.scidb.org/ http://www.scidb.org/ ) are all implemented in C++, although they aren't all necessarily object oriented C++).
- pcwalton 12y ago> if you do silly things like use raw references you can still get screwed.. experienced C++ programmers don't fall to that pit either Yes, they do. Empirically.
- AnimalMuppet 12y agoWell, he describes the FQA as "an overly combative reply to Marshall Cline's fairly combative FAQ". He's not claiming impartiality. But yes, it does look like he's gone very far out of his way to try to find things to complain about.
- skrebbel 12y agoIt was just an example. You're completely doing the double standard thing the OP is going on about. PHP and C++ are very comparable in how much they nearly encourage programmer mistakes, yet somehow in PHP it's the language's fault, and in C++ the programmer's just an idiot.
- cbsmith 12y agoThe tough one in C++'s case is that it isn't as easy as you think to critique the choices that were made. In most cases they are consequences of following design principles (and ones that seem good) through to the end (and you may disagree with the design principles, but then you probably shouldn't use the language). With PHP, that isn't the case and Rasmus would be the first person to tell you that.
- jordigh 12y agoFTFA: I have observed someone do this: _myarr[5]={0}; – they had in the .h file the definition int _myarr[5] and they remembered that this thing could be initialized with {0} in other contexts. What they did wouldn't compile in C++98; in C++11 it promptly assigned the int 0 to the non-existent 5th element of _myarr, and the usual hilarity ensued. Imagine how PHP would be ridiculed for this kind of little behavior – and PHP at least would never overwrite an unrelated variable with garbage. Imagine how with C++, the poor programmer will be ridiculed instead. Let me repeat that one more time, the poor programmer will be ridiculed instead. From your response, (and you shouldn't be having char pointers for strings anyway) [...] experienced C++ programmers don't fall to that pit either This is what the article is about, about what you are doing again. C++ is weird, it has a bunch of unusual behaviours, and when pointed, people who fall into those traps are the ones ridiculed, instead of the language. I don't know if C++ could have been made less weird with the constraints that it had, as others have pointed out in this thread (C compatibility with no overhead). Perhaps it couldn't be. But this article is unhappy with the attitude of defending C++'s weirdness and then blaming the programmers for falling victims to this weirdness.
- maxlybbert 12y agoIt isn't so much that you ridicule the programmer; it's a question of telling the programmer "don't use these things that you see mentioned in old books, we have replacements for them now." So, yes, comparing char*s is very different than calling strcmp; and if you want to use "==" to compare strings (so you don't have to remember to call strcmp), then use std::string.
- deong 12y ago"Blame" is too strong of a word, but the basic idea is that C++ provides a lot of power that PHP doesn't provide, and the price for that power is complexity. So while both languages are full of dark little corners waiting to kill you, C++'s dark little corners are there because really smart people couldn't figure out how to avoid them in all cases while still keeping the mandated design requirements, and PHP's dark little corners appear to be there because no one bothered to think anything through before banging out an interpreter. This isn't to say C++ is the best it could be. There are plenty of outright mistakes. But on the whole, C++ is weird and complicated because it had to be, at least to a much greater degree than PHP.
- ChrisAntaki 12y ago> And Stroustrup says about himself that he "lacks marketing clout" and says Java and C# are bad for you because they're "platforms, not languages", whatever that means. Perhaps Stroustrup is referring to the JVM and CLR, respectively?
- fiatmoney 12y agoRight. Java-the-language depends heavily on JVM-the-environment to be usable. Without an excellent GC (sometimes even with one) common coding patterns are unworkable, and the design of the language lends itself heavily to GC-dependent overall designs. There are also aspects of the implementation details that dominate language design (eg, primitive boxing and the generics implementation). That said, if you know what you're doing it's a great platform.
- nly 12y agoAfter reading this I recommend people go watch this recent interview with Bjarne[0] for some insight on the compromises taken by C++, a spot of history with regard to its development in tandem with C, and what he thinks of the 'competition' between new languages. I think it's only fair seeing as this article accuses Bjarne of "evilly shrewd" moves, maliciously crippling Cs development, and "knocking C it in to a coma". So watch it for Bjarnes perspective from his own mouth. You may as well see get a biased view from both sides. https://www.youtube.com/watch?v=jDqQudbtuqo https://www.youtube.com/watch?v=jDqQudbtuqo
- dmunoz 12y agoBjarne has been on a bit of a promotion tour recently. Here is another Q&A session he did with Slashdot on the 20th [0]. I suppose this is because of the recent standardization of C++14. One thing I don't particularly agree with is that some of his examples of C++ code use features that will not be made available for years, likely around or after 2017 when C++17 should be standardized. This is assuming compilers implement the standard quickly, which is worse on some platforms than others (cough Visual Studio cough). What I'm specifically talking about is any example that mentions concepts, but at least he is clear that concepts are only currently available in a branch of GCC. [0] http://features.slashdot.org/story/14/08/20/011231/interviews-bjarne-stroustrup-answers-your-questions http://features.slashdot.org/story/14/08/20/011231/interview...
- nly 12y ago> What I'm specifically talking about is any example that mentions concepts, but at least he is clear that concepts are only currently available in a branch of GCC. The syntax for concepts seems pretty well set now, and Alex Stepanov (author of the STL, and worked with Bjarne on the new concept model), in his Amazon A9 lectures (available on YouTube) recommends you simply #define concept names to "typename" so they at least exist in your code as documentation. The most important thing however is really to get people writing their generic code with a concious awareness of what their type requirements actually are, and then to document and specify them correctly, just like you'd rightly expect a function written in C99 to specify clearly what the pre and post conditions are... something the compiler can't help you with even today.
- copx 12y agoI do not see a double standard. There is a fundamental difference between PHP and C++. PHP was simply a bad design, needlessly ugly. In contrast, the design of C++ was constrained by the goals of - Compatibility with C (and later previous C++ standards) - Zero overhead / you do not pay for what you do not use - No GC Those constrains are responsible for most of its ugliness, not poor design.
- MereInterest 12y agoI would label your third point as a subcategory of the second, as it is impossible to have a garbage collector without having some sort of overhead.
- cbsmith 12y ago> I would label your third point as a subcategory of the second, as it is impossible to have a garbage collector without having some sort of overhead. It's impossible to have a heap allocator without overhead. How much overhead you have depends on the design constraints and the trade offs. Particularly if you are dealing with more sophisticated constraints like memory fragmentation and other issues, a GC might actually be an easier and lower overhead way of solving the problem. More importantly, you missed the most important aspect of the second point: "you do not pay _for what you do not use_". You and copx would be surprised to know that pretty much since the standard committee started working on it, garbage collection has been something on the table for C++, but they've never been able to get a proposal that everyone was happy with. I also think that the "No GC" category is perhaps not the right name. What C++ was really about was making UDT's first class types and therefore supporting value semantics. A side effect of that is that GC isn't nearly the priority it might otherwise be.
- klodolph 12y agoHow about we put it this way: all forms of dynamic memory management have overhead, including malloc(). The overhead for garbage collection is different from the overhead for malloc(); GC is worse some respects (latency, space usage) but better in other respects (throughput, development time). GC can be much faster than malloc() when allocating objects, depending on the GC scheme used and the heap profile, allocation savings may outweigh the cost of collection. So "No GC" is a completely separate point.
- possibilistic 12y agoI've been using C++11, and I really like it. Together with RAII it seems entirely modern and reasonable. Is there a list somewhere that describes what the "bad parts" (in the Crockford sense) of C++ are? Bad practices, bad feature interplay, etc. I've been thinking of writing a little C++ transpiled language of a limited feature set to make prototyping certain things easy. It would try to follow the accepted best practice conventions. I imagine it to be Coffeescript-like, with no separate header files, support abbreviated syntax, syntax for managed pointers, etc. It would probably follow a rudimentary linking scheme. It might be opinionated in ways that turn some people off: const vars and args by default, virtual methods by default, auto virtual dtors, and probably other "sensible" (in average usages) default patterns. Finally, I would want to generate simple intuitive interfaces that are linkable to existing C++ headers. Anyways, this would just be a side project for fun. I don't do C++ professionally. Just for robotics and lasers and hobby stuff. Any information on best practices and bad C++ usages would be awesome to have if I undertake this.
- klodolph 12y agoC++ is somewhat notorious its developers' practice of selecting different subsets of C++ as "the good bits". I think one of the biggest mistakes of C++ was conflating the three different concerns of record types, polymorphism, and encapsulation. All three are bundled up in the catch-all "class", and everyone comes up with their own workarounds which should have been language constructs to begin with. So you'll end up with a language which enforces your C++ style. The good bits everyone agrees on are parts like <algorithm>, and oh man, I have a hard time imagining how you'd make that easy to follow.
- deleted 12y ago[deleted]
- possibilistic 12y agoI hear the "different C++ subsets" meme all the time. To the best of my knowledge, this is typically cited in relation to template generics, exceptions, multiple inheritance, and RAII being used incongruently. IIRC, Google chose not to use exceptions, but this might be unrelated. (ex-googlers I know have mentioned that Google doesn't care about C++ services terminating and having to be respawned) Your comment kind of makes me question my understanding of the different subsets issue... Would you mind elaborating? I could really stand to understand this better. If you can't expand on it, thanks for giving me something to look into. :)
- putzdown 12y agoThese kinds of rants sadden me. They purport to outline absolute, logically inviolable reasons that a given language is "bad". What they really do is express the writer's frustration, an emotional reaction that probably emerged from working with language attributes and features that he in some cases doesn't understand well or in other cases simply doesn't prefer. C++ works beautifully for millions of programmers who happen to "get" it and like it, just as Java does for others and C# does for others and Haskell does for others and fill in your main squeeze here. Programmers are trees (http://www.jeffwofford.com/?p=915 http://www.jeffwofford.com/?p=915). We each adapt to what we like and come to like what we adapt to. Let's not pretend we alone have the One True Way or that our non-preferred way is stupid.
- izacus 12y agoWhile your point may be valid... let's also not pretend that some genuinely bad solutions are just "non-preferred" ways and that C++ is not without it's shortcomings and hidden pitfalls. After all, there is a reason that any style guide for C++ will include a large list of features that should not be used. I love the language and use it alot... but sadly, in many ways, this FQA is just too true.
- yid 12y agoYou're just reinforcing the parent's point. Take this sentence: > <PROGRAMMING_LANGUAGE> is not without it's shortcomings and hidden pitfalls. After all, there is a reason that any style guide for <PROGRAMMING_LANGUAGE> will include a large list of features that should not be used. This holds true of pretty much any language. Take a look at Google's extensive style guide library [1]; they all contain language features that should be avoided for any language. It all just reflects the iterative nature of programming language design. [1] http://code.google.com/p/google-styleguide/ http://code.google.com/p/google-styleguide/
- jzwinck 12y agoLook at Google's Python Style Guide: http://google-styleguide.googlecode.com/svn/trunk/pyguide.html http://google-styleguide.googlecode.com/svn/trunk/pyguide.ht... There is almost nothing in there that sounds like a landmine. Rather, some general guidance about "don't use global variables," "x may not be thread safe," and "don't write overly complex lambdas." My favorite "gotcha" in Python is using e.g. a list as a default argument. That's in the guide, and it sucks, but Python has surprisingly few such problems for newcomers.
- eslaught 12y agoSo the original guide seems not to have been updated since 2009 [1], and even then only in minor ways. Are these criticisms still valid of C++, even without the C++11 standard? Clearly the language issues won't have gotten any better (without C++11), but many of the arguments in the FQA seem to call into question the correctness of the compiler or the quality of the compiler's optimizations (see e.g. [2] and [3]). Is this still valid for modern compilers (GCC 4.8, Clang 3.4, etc.)? [1]: http://yosefk.com/c++fqa/changelog.html http://yosefk.com/c++fqa/changelog.html [2]: http://yosefk.com/c++fqa/ctors.html#fqa-10.9 http://yosefk.com/c++fqa/ctors.html#fqa-10.9 [3]: http://yosefk.com/c++fqa/templates.html#fqa-35.10 http://yosefk.com/c++fqa/templates.html#fqa-35.10
- rwallace 12y agoNo. There was a time when you couldn't count on a lot of C++ features being fully or correctly implemented, but that time has passed. A lot of the other arguments revolve around slow compile times, which are fixed by having plenty of fast hardware. Most of the remainder are divided between matters of taste, and genuine sharp edges you have to be careful to avoid.
- deleted 12y ago[deleted]
- overgard 12y agoProfessional C++ programmer here, although previously I mostly worked in C# and python. I have to admit, I'm pretty ambivalent about the language. For a long time I loved it, and then for quite a while I really hated it (tracking down segfault that only happens on a release build on a tight deadline will do that to you). The thing is, all the terrible things people say about C++ are true; but what else is there in that space? It seems like most language design is aimed at a higher level. D seemed promising, but the whole standard library issue seemed to derail its momentum, and as nice as the language is, it's really complicated and the tool support kinda sucks. Rust is interesting but too immature to sell to the decision makers at most companies. I understand people going back to pure C, but man, as screwed up as C++ is it's hard to give up things like vectors and strings and namespaces. Everything else has a memory model that doesn't quite work, and look, I like GC as much as anyone but sometimes you need a language where you can turn it off when you need to guarantee there isn't going to be intermittent GC pauses.
- TheDong 12y agoGoogle's go and Mozilla's rust both fit the same sort of space. Rust especially.