Live data from Hacker News

How to Adopt Modern C++17 into Your C++ Code [video]

youtube.com

121–124 of 124 posts

Re: How to Adopt Modern C++17 into Your C++ Code [video]

#121

Earlier quoted context omitted.

> If somebody else owns it and you're working on the whole string `const std::string&` is probably your best bet Actually string_view is more general. You can build one out of a string literal, a vector , some memory from a memmap call, and an unbounded list of other things. If an interface uses a `const std::string&` and you don't have one, you have to, implicitly or explicitly, copy the data into a `std::string` to…

Is it just me, or does it seem like std::string_view is just an alternative in semantics to a `char *` plus a size? It doesn't null-terminate (which would require copying or mutating). Is there some extra safety checking this provides?

- It has range semantics, so you can use it with standard algos or range for.

- It encodes the invariant in a type (char * points to at least size bytes, no expectation of null termination, no ambiguous definition of an empty range)

- The size always travel with the pointer so no chance that it gets separated.

- syntactically simple conversion from string (or other string-like entities) to string view reduces clutter in the code.

I consider lack of null termination a plus.

Re: How to Adopt Modern C++17 into Your C++ Code [video]

#122
post #61
post #59

Earlier quoted context omitted.

> I completely understand why (we're learning low level stuff and it's worked for the last 30 years so whatever) Are you sure it's not just because your teachers are not (enough) aware of those new features ? Because I work in a University and know way too many colleagues that still use new/delete on a daily basis.

I believe you are correct, but even the textbook ignores the new stuff except for nullptr and briefly mentioning range based for loops. The professor who taught my Data Structures class (who is an exceptionally great teacher BTW) actually uses C in most of his own work, so we even learned a little bit about how to simulate recursive calls using a void* stack and goto. I hope my peers don't go out and write code like…

Learning how it used to be done isn't a bad thing, though. Because when you need to maintain code that existed before modern C++, of which there is much, you'll be ready.

Re: How to Adopt Modern C++17 into Your C++ Code [video]

#123
post #57

::Somewhat tangential anecdote and plea for advice:: I just finished my third semester of using C++ (with Data structures 1) and we use NO modern stuff in class [1]. Raw pointers. New and delete. No Lambdas (although we learned about function pointers, though not function objects). I completely understand why (we're learning low level stuff and it's worked for the last 30 years so whatever), but now I'm trying to ram…

The largest change is ownership semantics. If you can learn or have learned ownership semantics in such a way that ties that abstract concept to concrete real life examples, most of your work will be done.

Modern day C++ has two ways to do most of the things you want to do: The C way, and the C++ way. eg, c array[] vs std::array, or char* vs std::string and the like. However, original C++ without C is still there and not updated. Eg, inheritance is a C++98 feature, not a C feature, so there is only one way to do inheritance for the most part.

It takes little time to identify what features are C features and what features are C++ features. From there you'll know when to look up alternatives and when not to, giving you an easy upgrade map.

Also, some C features are more powerful, so it is a matter of choosing the right tool for the job. For example, looping through a data structure and adding elements. Doing so invalidates iterators, but works will with an index, so it might be ideal to use the old C way in situations like that. This also adds explicitness to code. If you're working in a modern code base and in the rare spots where the C way is done, you can get an idea why and from that what the code is doing without having to read all of it. This makes reading code easier once both C and modern C++ have been learned.

Re: How to Adopt Modern C++17 into Your C++ Code [video]

#124

Earlier quoted context omitted.

FWIW, I used to be the lead of a C++14 RTOS. You can be low level and still use a lot fo these techniques. And that void*/goto hack sounds bad, even in C land. That's basically only acceptable in a byte code interpreter.

well, if you have to implement a non tail-recursive function and want to have control of the max recursion depth (i.e. not get killed because you have exhausted the stack) it is an option.

There you should probably pass a iteration count into the function rather than some weird goto hack.
Post reply on HN