Live data from Hacker News

Modern C++ for C Programmers: part 5

ds9a.nl

71–80 of 82 posts

Re: Modern C++ for C Programmers: part 5

#71
Does anyone know what this means?

"Such magic does not come for free however. If we look ‘inside’ a std::shared_ptr, it turns out it carries a lot of administration. First there is of course the actual pointer to the object contained. Then there is the reference count, which needs to be updated and checked atomically at all times. "

My impression was that simply dereferencing, which is going to be most of the use of it, is not checking that counter at all. Only things like assignment modify it, which should be more rare.

Re: Modern C++ for C Programmers: part 5

#72
post #6

I wish I had a project that warranted C++ right now. It’s a magnificent language.

Why not implement standard data structures? Or system programs ( ls, ping, etc )? Or a compiler ( a subset of C perhaps )? Hell a basic http server or sqlite clone? There's an endless list of projects you could do in C++ if you really wanted to.

Re: Modern C++ for C Programmers: part 5

#73
post #66

auto ptr = mmap(NULL, sizeof(std::atomic ), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); g_counter = new(ptr) std::atomic (); waw, a 4k integer (or even more, depending on the platform).

I don't see the problem here. The code you quote is correct and guaranteed to allocate the minimum amount of memory for which the hardware can support the requested functionality. It passed sizeof(std::atomic ) as the length parameter and mmap() decided to give us a larger page (which may or may not be 4k) for its own very good reasons, namely that the overhead of keeping track of hyper-granular memory pages would co…

Well, there's not a lot of context to say whether there are more shared counters. That said, I overlooked the MAP_SHARED, and the code actually got itself in non-portable land. Not all atomics are actually atomics. IIRC, at least ARM doesn't have 64-bits atomics, and using atomics on such platforms means using locks. Which are very likely to not work properly in shared memory.

Re: Modern C++ for C Programmers: part 5

#74
post #7

Please please please can we get rid of headers and have modules? I find it super annoying having to specify things half in one place and half in another. Unfortunately, there still seems to be a bit of disagreement on the implementation among the standards committee.

You do not really to do that in C. If you are careful, the header only contains the visible interface, which is a list of functions that can be trivially deduced from the non-static functions of the source file. Moreover, the header file is optional, and is never included by the source file. In C++, yeah, this language sucks.

> Moreover, the header file is optional, and is never included by the source file.

That is totally opposite to my experience. Example from Linux:

https://github.com/torvalds/linux/blob/master/kernel/smpboot... https://github.com/torvalds/linux/blob/master/kernel/smpboot...

Re: Modern C++ for C Programmers: part 5

#76

Earlier quoted context omitted.

You do not really to do that in C. If you are careful, the header only contains the visible interface, which is a list of functions that can be trivially deduced from the non-static functions of the source file. Moreover, the header file is optional, and is never included by the source file. In C++, yeah, this language sucks.

> Moreover, the header file is optional, and is never included by the source file. That is totally opposite to my experience. Example from Linux: https://github.com/torvalds/linux/blob/master/kernel/smpboot... https://github.com/torvalds/linux/blob/master/kernel/smpboot...

Yeah. If you don't include the header from the source file you will inevitably end up with differing function signatures in the header and source files because the compiler won't be able to compare the declaration and definition to each other. Such a program may even link.

Re: Modern C++ for C Programmers: part 5

#77
post #7

Please please please can we get rid of headers and have modules? I find it super annoying having to specify things half in one place and half in another. Unfortunately, there still seems to be a bit of disagreement on the implementation among the standards committee.

You do not really to do that in C. If you are careful, the header only contains the visible interface, which is a list of functions that can be trivially deduced from the non-static functions of the source file. Moreover, the header file is optional, and is never included by the source file. In C++, yeah, this language sucks.

You can do the exact same thing in C++: https://en.cppreference.com/w/cpp/language/pimpl

Re: Modern C++ for C Programmers: part 5

#78

Does anyone know what this means? "Such magic does not come for free however. If we look ‘inside’ a std::shared_ptr, it turns out it carries a lot of administration. First there is of course the actual pointer to the object contained. Then there is the reference count, which needs to be updated and checked atomically at all times. " My impression was that simply dereferencing, which is going to be most of the use of…

Edit: it looks like that comment on the blog is incorrect. You only incur overhead from copying or going out of scope.

Re: Modern C++ for C Programmers: part 5

#79

Earlier quoted context omitted.

If you have your IDE properly configured with the include dirs needed to build, at least MSVC can autocomplete for you. Personally, I don't rely on this, as I do most of my C++ work from the command line using bash, vim & make. Can't explain why, but for C++ projects, I intuitively remember what paths I need for headers, but for a C# project, I rely upon autocomplete as a crutch. Maybe it's just how much more time I…

> If you have your IDE properly configured You're misunderstanding me. I'm saying the IDE should discover headers for me . MSVC refers me to a Byzantine series of clicks, drop downs, text input, and OK buttons. That interface hasn't changed for 20 years A proper modern UX would scan for valid headers in a dir, then allow me to choose which ones to "import" into the workspace. Not make me hunt for them through an anti…

well, for instance, in Qt Creator, if you have a type not available you can right click on it and it will propose a list of headers to include it from that it will add in the file.

Re: Modern C++ for C Programmers: part 5

#80

Earlier quoted context omitted.

Single-file header libraries are actually all the rage in C++ these days. Here's[1] a list even! [1] https://github.com/nothings/single_file_libs

This is what you get if you're missing a proper package management that allows users to easily install their libraries. That single file is the substitute for a package that you can simply put somewhere, include it and it works.

The package manager of my OS does fine.
Post reply on HN