Live data from Hacker News

Does C++ still deserve a bad rap?

nibblestew.blogspot.com

121–130 of 310 posts

Re: Does C++ still deserve a bad rap?

#121

Like every programming language out there, C++ is a tool. And like every tool out there it has its uses. There isn't any point of using C++ to count words in a text file. Any high-level language like Python will beat you to it. However, there's one thing you can do in C++ and not in Python or JavaScript or PHP: fully control the memory layout of your data. While you don't need it in most of the cases, it becomes a ki…

> * Try implementing an on-disk hash table in Python and you're stuck manually packing ints and longs in an out of arrays. In C++ it could a simple template used with a memory-mapped file. This is only simpler if you don't need to handle read failures. If you want to handle read failures, memory mapping can quickly become way more complicated. You have to install signal handlers and do a complicated dance to correctl…

Indeed. Memory mapped files are the bubble sort 2.0.

They look simple and approachable on the surface, lead to an elegant code and seem to solve the problem in basic test cases, but in practice they have nasty scalability problems. In particular, trying to work with very large files leads to cache thrashing that affects the entire system, which makes mmaped files particularly unfit for implementing off-memory data structures. Moreover, the code has zero control over this behavior, because we ultimately over-allocate memory and outsource swapping details to the OS.

Re: Does C++ still deserve a bad rap?

#123

Earlier quoted context omitted.

> There isn't any point of using C++ to count words in a text file. Any high-level language like Python will beat you to it. However, there's one thing you can do in C++ and not in Python or JavaScript or PHP: fully control the memory layout of your data. Any books or resources for digging deeper in these kinds of topics? I’m a Python developer and would like to learn C++ but the just the reasons you mentioned make i…

To get a quick start, try declaring some class variables and then open the Memory window and go to "&variable_name". You can also evaluate "sizeof(variable_name)" to get its exact size. You'll see a bunch of bytes reflecting how exactly your variable is seen by the CPU. That's exactly how many RAM bytes it occupies. It can quickly copied to a new location, limited only by your RAM bandwidth. It can be dumped to a dis…

How does this help?

Re: Does C++ still deserve a bad rap?

#124
post #89
post #45

Earlier quoted context omitted.

you can malloc() to get a memory range, or mmap() to get a disk-backed memory range with kernel-managed paging, and then you can do whatever you want with it. You get a pointer to the start of the range, and you know how large it is, so from there it's just pointer arithmetic and assignment. Easy peasy. Now you just have to make sure that you zero data when you're done with it, track how much of that memory is availa…

> you can malloc() to get a memory range, or mmap() to get a disk-backed memory range with kernel-managed paging, and then you can do whatever you want with it. No need for that, as C++ provides placement new(). https://en.cppreference.com/w/cpp/language/new

This doesn’t really solve the same problem, even though the two often go hand-in-hand.

Re: Does C++ still deserve a bad rap?

#125

“There are only two kinds of languages: the ones people complain about and the ones nobody uses.” Bjarne Stroustrup

I don't hear too many complaints from C users. Perhaps because C never overpromised?

I understand C++ is messy because, back in the day, Stroustrup never say no to a request or suggestion from a potential user. He wanted his language to be widely used. Guess he succeeded.

Re: Does C++ still deserve a bad rap?

#127
post #43

Earlier quoted context omitted.

"Most people will say foo and bar are functions and that's a reasonable guess. Unless you are talking about C++. In C++ foo and bar could be functions but could also lots of other things." Same as in Lisp, one of the most beloved of languages on HN. Were Lisp's macros a mistake? Some will say yes, but overall the language is more praised for them than damned. So highly regarded are Lisp macros that many languages wil…

I don't think lisp is that beloved here. It's just that the few people who love it love to bring it up.

Hacker News is written in a Lisp, started by people deeply interested in the language, and frequented in a large part by people who feel similarly.

Re: Does C++ still deserve a bad rap?

#128

Earlier quoted context omitted.

It is possible with a regex iterator #include #include #include int main() { std::string s = "Python*C++*Java"; std::regex regex("\\*"); std::vector out(std::sregex_token_iterator(s.begin(), s.end(), regex, -1), std::sregex_token_iterator()); for (auto &s: out) { std::cout

Hey just curious i have only coded c++ in school I’m curious to know why don’t we use `using namespace std;` in real world when writing c++

Because it imports literally everything into your current scope, and the standard library types are named in such a way that they will likely conflict with code you write. Named a variable “min” or “max”? Function called “list”? You’re going to cause problems for yourself.

Plus, using std:: is generally a good reminder that you’re using the standard library types and not some homegrown facsimile of that type, as is common in many C++ projects.

Re: Does C++ still deserve a bad rap?

#129

C++ is, quite rightly, considered an overly-complex and bloated language. A lot of newer features are papering over poor design from the beginnings of the language. But.. There is something about the use of destructors, RAII, and even smart-pointers that just feels so...elegant? I'm not sure what the word is I'm looking for. I feel like there's a kernel of a beautiful language hiding in c++ and those features are the…

Rust does a good job capturing that kernel.

Re: Does C++ still deserve a bad rap?

#130

Earlier quoted context omitted.

It is possible with a regex iterator #include #include #include int main() { std::string s = "Python*C++*Java"; std::regex regex("\\*"); std::vector out(std::sregex_token_iterator(s.begin(), s.end(), regex, -1), std::sregex_token_iterator()); for (auto &s: out) { std::cout

Hey just curious i have only coded c++ in school I’m curious to know why don’t we use `using namespace std;` in real world when writing c++

Because it imports everything from the std namespace and can lead to name clashes. It also makes it harder to see where symbols come from. If you see `std::vector` you know exactly what it is. If you just see `vector` in a large code base it could be anything. Could be a hand-rolled version of std::vector from prehistoric times or people not trusting the STL for no reason. Could also be a mathematical vector or something for vector graphics or whatever else. That is why people generally always fully specify where things come from, it's not limited to just the STL.

`using namespace X;` is somewhat fine if you limit it to a scope by using it in a source file and ideally only the function body where you use it, in header files it is a really bad idea because then the namespace gets exposed to everything including that header. That can lead to the compiler calling the wrong function and you might not even notice it.

Post reply on HN