HN Simulatornew | past | comments | lists | submit | moltopoco's commentslogin

Just one data point, because I have to get it off my heart. I loved Modern C++ when it meant C++11, but I've become more negative with each new language standard reaching my workplace.

Using string_view over string by default is probably the #1 cause of memory errors that landed on my desk. I also keep hearing that "int x{0}" is preferable to "int x = 0" which allows for super dangerous implicit conversions, but then the same people rewrite push_back into emplace_back, which is much more lenient w.r.t. type conversions. It is also much easier to litter every function with [[nodiscard]] constexpr nothrow than it is to argue for readability.

My best explanation is that "Modern C++" is making developers biased towards whatever language feature is newer, instead of making good decisions. I wish the community gave up on the moniker.


`string_view` (and `std::span`) is great! Finally I don't have to use concrete types like `const std::string&` or `const std::vector&` as function parameters when all I really want is just something that behaves like a string or array/vector type.

In fact, `std::string_view` can lead to very elegant and efficient (zero-allocation) string parsing code.

IME, taking a string_view as an argument is pretty unproblematic. Two areas where I have seen problems:

1. returning a string_view from a function. Think twice before doing that.

2. calling data() and passing the result to a function that expects a null-terminated C strings. (std::string_view is not guaranteed to be null-terminated.)

---

Apart from that, there are so many great features and libraries in C++17 and C++20 that I use on a regular basis and that I would really miss:

- structured bindings

- if-statement initializers

- fold expressions

- inline variables (very useful for handling global state in header-only stuff)

- if constexpr (replaces many uses of SFINAE)

- concepts (replaces all of the remaining uses of SFINAE)

- coroutines (really nice to use with ASIO)

- std::optional

- std::variant

- std::filesystem

- std::format (although I personally stick to fmt::format)

- from_chars() and to_chars() (fast, local-independent string-number conversion routines)

- std::bit

- std::apply, std::invoke, std::bind_front

- etc.


I have no problem with any of these features, only with the culture surrounding them. The point should be to write maintainable, safe, and efficient code, but sometimes that is at odds with writing "modern" code because the boring old C++11 way works just fine. In my anecdotal experience (YMMV), "modern" then often trumps other concerns when it should never have been a goal in itself.

Most of the features I have listed make the code more readable, which I would count as more maintainable. None of them have a runtime overhead.*) In fact, `` and `` are significantly faster than their counterparts.

Personally, I don't use any modern C++ features just for the sake of it. That would be silly indeed! For example, I have never used ranges/views because I don't find them particularly compelling, at least for things I do.

*) Creating a coroutine frame typically requires a heap-allocation, but so does the old ASIO callback style.


We may be in agreement, so I'm sorry to split this hair: These features don't make the code more readable. They _can_ make it more readable.

You can also replace a simple overloaded function with an implicit template that uses modern "if constexpr" to distinguish between input types. Or you can "modernize" a legacy function that takes a const string& to take string_view instead, even if the very first thing the function does is to turn it back into a string to pass its c_str() to a C API (leading to more net allocations). And so on.

I always found the fashion trends of the C++ community counter-productive, and that is what I personally associate with the term "modern C++" nowadays (since around C++20). It was the same with iterators after C++98 - cool, now we can express algorithms with all the clarity and safety of C pointer arithmetics. With all due respect to the original STL, std::erase_if(cont,pred) is a godsend in its simplicity, but I suspect it would have been against the zeitgeist for a long time.


> They _can_ make it more readable.

Fair enough!


> IME, taking a string_view as an argument is pretty unproblematic. Two areas where I have seen problems

I've seen a few problema with automatic conversion of const std::string& arguments to std::string_view. They arised when the argument was later captured by value in a lambda and passed as an argument to e.g. std::thread. Instead of safely passing copy of the string now you pass refwrence to a memory owned by a different thread, which may be deallocated at any moment.


This can be prevented by `[[clang::lifetimebound]]` and I've been doing that lately.

> all I really want is just something that behaves like a string or array/vector type.

What does behave like a std::string/std::vector but is not std::string/std::vector, respectively? In my experience, if you want a flat array behind a T* (or std::dequeue at the worst) — so that the autovectorization can kick in — then that's precisely what you want, and no generalization to e.g. a linked list, or a heap, or a red-black tree is really possible.


> What does behave like a std::string/std::vector but is not std::string/std::vector, respectively?

`std::span` works with all vector/array-like types: `std::vector`, `std::array`, C-arrays and third-party vector/array types.

Similarly, `std::string_view` works with `std::string`, C-strings and third-party string types.

> so that the autovectorization can kick in

It's not even about vectorization. Often you just have a function that takes a string-like or array-like thing, but you don't care about the exact container. Yes, you could make the argument a template (and constrain it with concepts), but then you have to put everything in headers.


To be fair, string_view has one very attractive feature over const ref string. String's char constructor is not explicit. With string view, you are guaranteed to not allocate by accident.

Exactly! When you have a `const std::string&` argument, passing a C-string implicitly allocates a new temporary `std::string`.

`std::string_view` accepts both `const std::string&` and `const char*` without hidden allocations.


Understood, and I also use string_view where it makes sense. But while overusing std::string can cause unnecessary and hidden allocations, overusing string_view often leads to memory errors.

I also love C++11 "auto", but overusing it then makes the string/string_view bugs harder to find.

I am perfectly happy with with idiomatic C++11 plus features from recent standards, but I don't feel like that counts as "modern C++" anymore. A sibling comment put it better: idiomatic C++20 has become its own separate thing, and I personally don't love it, and it is certainly not more robust in my experience.


> I also love C++11 "auto", but overusing it then makes the string/string_view bugs harder to find.

Yes, I have soured a bit on the "almost always auto" mantra myself. There are cases where explicit types are both clearer and safer.


When I've had a coffee too much, I notice the 120 Hz flicker even more. But even when I'm tired and don't, my mood still noticeably increases when I leave flickery spaces. (I sometimes test my hunch with the slow-motion cam as I am leaving.) I've tried looking for studies to prove or disprove my experience. They should be easy to find, right? Humanity is replacing the entire planet's lighting with LEDs after all! Comment sections have been busy with people arguing about PWM flicker for years, probably longer. What is the best study you can find? For me, it is always the one from 1989 about pre-LED 60 Hz office lighting (that said it had adverse effects). Seriously...?

I wish I was a Bitcoin billionaire, for many reasons, but doing another study on flickery LED lighting seems like the lowest-hanging fruit of our entire civilization. I live in a country that is regulated to hell and back, and yet even our street lights flicker visibly, not to mention classrooms, children's bedrooms, and the screens that people stare at for hours every day. I don't get it.


When I'm tired or had too much coffee I feel the same effect: it's like my brain / eye framerate drops overall but at the same time noticing flicker more. It's especially noticeable at night.

I think it could also be related to the muscles in the eyes not stabilizing the vision as well. Those muscles work extremely hard, even correcting rotation partially. It's really cool to see in slow motion, and I bet you'd see a difference in the jittery or tired state under PWM light too.


Guidelines | FAQ | Lists | API | Security | DMCA | Apply to YC | Contact

Search: