Live data from Hacker News

Popular Myths about C++, Part 3

isocpp.org

51–60 of 114 posts

Re: Popular Myths about C++, Part 3

#51
Bjarne should know better than to directly compare the performance of std::sort and qsort. One is typically printed in full in a header file, while the other is typically compiled separately. If qsort were found in a header file, it could be inlined into identical code to the C++ version, regardless of the fact that there are void pointers lying around everywhere.

There are caveats: the compiler might not choose to inline unless you force it to, and if you do that then you'll end up with duplicate code in the case of multiple calls with the same comparison function, while C++ can automagically merge duplicates (although you probably want to write a wrapper function anyway, and C++ will still waste compile time generating the duplicates if the calls are in different source files). Also, if the sorting function calls a secondary function in multiple textual locations, and that function is significant enough that inlining it would produce wasteful code, the pure inlining-based approach will be insufficient (but I don't think most sorting algorithms do this).

In other words, C++ makes it easier to do this sort of thing. No surprise! It certainly makes it prettier. But when it comes to performance, in practice the above would likely not be a big deal for qsort, so the difference between the two functions is really more a matter of convention regarding the implementation location. Benchmarking the two and explaining only that type safety "makes for excellent inlining and good optimizations" is simply misleading.

Re: Popular Myths about C++, Part 3

#52

One of the reasons people use "low level code" for performance is because the STL doesn't easily provide control of memory which is critical to performance. Electronic Arts wrote their own version of the STL largely so they could better control memory [1]. I'm not really sure about the rest of the myths. I'm a little confused about how "To understand C++, you must first learn C” is a myth since C++ is a superset of C…

There's an example in Part 1, where string concatenation is used as an example. C++ requires "adding" two string objects and C requires manipulating char pointers. And thus, C++ is a better teaching programming language.

Although true, I feel this argument is rather weak: it's true, that when teaching I wouldn't want to start with pointers and malloc's from the get go, but it does not mean C++ is the only alternative.

Re: Popular Myths about C++, Part 3

#53
post #38
post #10

Earlier quoted context omitted.

"C++ sucks" has become a bit of a meme around here. The upsides outweigh the downsides for a surprising number of cases. Every alternative is either immature, doesn't solve the use case that needs C++ or doesn't have the required libraries/tools.

Another meme / idiom seems to be "Why use C/C++ when you can use Rust?"

Sounds better than labelling anything you've seen twice as a meme/idiom/myth/design pattern, etc.

Re: Popular Myths about C++, Part 3

#54
post #52

One of the reasons people use "low level code" for performance is because the STL doesn't easily provide control of memory which is critical to performance. Electronic Arts wrote their own version of the STL largely so they could better control memory [1]. I'm not really sure about the rest of the myths. I'm a little confused about how "To understand C++, you must first learn C” is a myth since C++ is a superset of C…

There's an example in Part 1, where string concatenation is used as an example. C++ requires "adding" two string objects and C requires manipulating char pointers. And thus, C++ is a better teaching programming language. Although true, I feel this argument is rather weak: it's true, that when teaching I wouldn't want to start with pointers and malloc's from the get go, but it does not mean C++ is the only alternative…

If you're teaching programming, don't teach C++.

If you're teaching systems programming, don't hide pointers. In this case, knowing the addition operator is useful iff they understand the underlying operations.... chances are, if they're learning C++, they don't.

Re: Popular Myths about C++, Part 3

#55

Earlier quoted context omitted.

False dichotomy. Plenty of people who "build really cool things with it" also bash C++, and rightfully so.

Nobody I know. People I know that use C++ every day will admit it has issues but are also realistic enough to understand that any language with the same design constraints and long history as C++ will be complex. It's the catbirds in the gallery that don't actually have to write the kind of apps that require a language like C++ that "bash" it.

Catbirds like Linus Torvalds, for example?

Re: Popular Myths about C++, Part 3

#56
post #27

"C++ is a big language. The size of its definition is very similar to those of C# and Java." I can't speak with authority to C#, but C++ is a massively larger core language than Java with far more complicated semantics.

This is one thing I wish C++ advocates would stop bringing up. While it's technically true that the page count of the C++ spec is comparable to that of C# and Java, not all pages are created equal. The C++ spec is incredibly dense, it's written in a very terse style and often packs as much information into a single sentence as other specs spend an entire paragraph on. Also, the Java spec in particular is typeset with…

Not to mention, the spec of Java has the benefit of being built on very firm semantics, regardless of how well they were designed. C++ has no such solid footing on which to stand.

Re: Popular Myths about C++, Part 3

#57

One of the reasons people use "low level code" for performance is because the STL doesn't easily provide control of memory which is critical to performance. Electronic Arts wrote their own version of the STL largely so they could better control memory [1]. I'm not really sure about the rest of the myths. I'm a little confused about how "To understand C++, you must first learn C” is a myth since C++ is a superset of C…

I guess that depends on your definition of 'easily' - you can provide custom allocator s fairly easily. What's more problematic are things like the memory usage patterns of std::vector and std::string. Once you know how they work you can avoid the pitfalls or use custom alternatives.

Things like alignment support didn't come out in g++ until 4.8 [1] but now you could start actually do most of what the EASTL is doing aside from the access to private members

[1] https://gcc.gnu.org/projects/cxx0x.html

Re: Popular Myths about C++, Part 3

#58

One of the reasons people use "low level code" for performance is because the STL doesn't easily provide control of memory which is critical to performance. Electronic Arts wrote their own version of the STL largely so they could better control memory [1]. I'm not really sure about the rest of the myths. I'm a little confused about how "To understand C++, you must first learn C” is a myth since C++ is a superset of C…

While the C++ language is indeed a superset of the C language, the paradigms of modern C++ have almost no overlap with those of C. See for example the string qsort vs string std::sort elsewhere in this thread.

The myth in "To understand C++, you must first learn C" is not that C and C++ are unrelated. The myth is that learning C helps you understand C++. The reality is that telling people to learn C first is an excellent way of getting them to write really bad C++ when they switch over to C++11.

Re: Popular Myths about C++, Part 3

#59
post #55

Earlier quoted context omitted.

Nobody I know. People I know that use C++ every day will admit it has issues but are also realistic enough to understand that any language with the same design constraints and long history as C++ will be complex. It's the catbirds in the gallery that don't actually have to write the kind of apps that require a language like C++ that "bash" it.

Catbirds like Linus Torvalds, for example?

Does Torvalds write anything in C++?

The kernel and git are in C

Re: Popular Myths about C++, Part 3

#60

One of the reasons people use "low level code" for performance is because the STL doesn't easily provide control of memory which is critical to performance. Electronic Arts wrote their own version of the STL largely so they could better control memory [1]. I'm not really sure about the rest of the myths. I'm a little confused about how "To understand C++, you must first learn C” is a myth since C++ is a superset of C…

Not as true any more. Are you familiar with value categories in C++11?
Post reply on HN