TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

The Dark Side of C++ (2007)

76 点作者 KonradKlause大约 14 年前

14 条评论

dkarl大约 14 年前
Again the operator overloading thing.<p>Yes, it's true. &#38;&#38; 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 &#38;&#38; operator. Yes, they could do it in some weird and unexpected way. Just like somebody can declare a weird and unexpected macro in C.<p>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.<p>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 <i>before</i> you use the type.<p>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.<p>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."
评论 #2549086 未加载
评论 #2549067 未加载
rgarcia大约 14 年前
I have to disagree with some of the "warts":<p><i>operator[] adds a member to a map if it does not already exist</i><p>The main theme of this article seems to be "C++ gives you too much flexibility," but this is a case where C++ is enforcing a default behavior and <i>limiting</i> your flexibility. In Python, for example, understanding code that queries for a key in a dictionary requires you to know some context about whether that dictionary has a default value for missing keys. For all the quibbling the OP does about operator overloading, I'd expect him to be grateful for the predictable nature of map's operator[].<p><i>local static initialization is not thread safe</i><p>This is just wrong: <a href="http://stackoverflow.com/questions/1270927/are-function-static-variables-thread-safe-in-gcc" rel="nofollow">http://stackoverflow.com/questions/1270927/are-function-stat...</a>
评论 #2549376 未加载
thedigitalengel大约 14 年前
Firstly, half of the time the guy bitches about flaws with the compiler, and not the languages (Page 0 to 6).<p>Most of the bitching is about language features which exist. If you don't like auto_ptr then DON'T use auto_ptr! C++ at least allows you to ignore features without paying a penalty. Which is why it has a virtual keyword; and does not make every method virtual like Java.<p>The at() versus operator[] is also an example of the above: bounds checking can get expensive in a tight loop.<p>It seems that the author has never seen a large, well-written C++ project. In fact, that is the case with many such C++ bashers (I used to be one too). Once one sees how C++ makes it easy to manage a 500000 line project, they will probably think again.
评论 #2549631 未加载
评论 #2549654 未加载
评论 #2549528 未加载
Peaker大约 14 年前
Many good criticisms, mixed with some bad.<p>Exception safety is achievable (pthread_mutex_lock example) if you wrap resources with RAII classes. If he called this "difficult to use C libraries while retaining exception safety" it would be more accurate.<p>The iterator criticisms are applicable to C iterators too (represented by pointers or user objects).<p>The various things `baz = foo-&#62;bar(3)` could mean are interesting. Some of them are reasonable, if you assume some sort of consistency. It may make <i>audits</i> hard, but the fact it might use implicit cast converters there should not make it harder to read if you are allowed to make the most basic assumptions about what an automatic cast operator would do.<p>I enjoyed it, nonetheless.
andos大约 14 年前
Some of the examples at the end are laughable.<p><pre><code> If new[] throws an exception, this leaks a file handle and deadlocks the next caller. </code></pre> Poorly written code is buggy? No shit, Sherlock! — to paraphrase the OP.<p>The Frequently Questioned Answers has good, insightful criticism of C++ is here: <a href="http://yosefk.com/c++fqa/" rel="nofollow">http://yosefk.com/c++fqa/</a>
KonradKlause大约 14 年前
BTW: The errata: <a href="http://www.fefe.de/c++/" rel="nofollow">http://www.fefe.de/c++/</a>
comex大约 14 年前
&#62; With C++, you don’t see the writes, because it says `some_func(whatever,the_int,SOME_FLAG);` and it can still write the_int if it’s a reference. In C, it would be &#38;the_int, which is easily recognizable.<p>I don't understand why people do this. C++ has not abolished pointers; it's still possible to require the &#38;, and it increases readability considerably; why ever use references (to value types; objects are different) as function parameters?
评论 #2549594 未加载
huhtenberg大约 14 年前
I tend read all "C++ is (bad|good)" lists and it never occurred to me to look at operator overloading as something inherently bad, something that sacrifies the clarity of the code for its brevity (see page 23 and onward). This is an excellent angle... but not that I would prefer C++ to <i>not</i> support overloading.
albertzeyer大约 14 年前
Many of the complaints have become irrelevant or at least much less relevant with C++11 and huge progress also on the compiler side (most notable clang but for example GCC also has improved many of the unreadable error messages).
评论 #2548732 未加载
评论 #2549393 未加载
评论 #2549113 未加载
dhruvbird大约 14 年前
&#62; Iterator to stale element (resized vector, or balanced tree after rebalance)<p>I don't think that rebalancing the tree invalidates iterators (other than the deleted element [if any] that is)
Groxx大约 14 年前
After seeing some of those rants, especially after the namespace-hatred, I wonder if they'd prefer a flavor of Assembly. Apparently they hate many common methods of simplifying and organizing code - clearly, things like virtual methods should always look different than non-virtual methods, namespaces are pointless when you can `gl_open`, and all macro-like functionality is simply obfuscation.
jfr大约 14 年前
Most, if not all, of his arguments were already beaten to death. The rebuttal should be floating around somewhere on the Internet, but I couldn't find it with a quick search right now.<p>Anyways, right at the beginning of the presentation you see the quality of his arguments as he uses a very known problem of a particular compiler (GCC) to bash the language. Sort of a straw man fallacy.
评论 #2548888 未加载
评论 #2548752 未加载
dhruvbird大约 14 年前
&#62; x[0] -= x[1]; // x[1] adds sizeof(bar) here, not sizeof(foo)<p>This code doesn't even compile?? What is the author trying to show with this example?
mattgreenrocks大约 14 年前
Please flag this article. Why do we feel the need to discuss how much C/C++ rocks/sucks repeatedly? HN should frown upon bikeshedding more than it does.<p>If I wanted an echo chamber, I'd be on Reddit.
评论 #2550125 未加载