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.
`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)
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.
> 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.
> 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.
> human understanding does not decrease, it just moves up a level or two.
It absolutely does. You lose understanding of the code, but what kind of understanding do you actually gain in turn? AFAICT it is purely a tradeoff between velocity and understanding.
That is true and while oss produced a huge amount of value for the world, the vast share of money went to paying developers to produce slop and build horrible buggy systems ala daily.wtf stories.
I'm not saying this in a sneering tone. I'm incredibly sad about this state of things. But the industry as I experienced it very rarely placed any value on quality code. So when I see ai writing code I don't see anything of value being lost. I suspect that a lot of high quality code will still get written by people who care and direct their agents to do so. Similar to how a carpenter can produce wonderful work with power tools but a regular joe will produce awkward wooden slop.
I can't speak for commercial software development, but that's very much not true for open source software. Personally, I don't want to use software that is a black box. That's the reason why I'm using FOSS in the first place.
Another note: some software has a history of multiple decades. In fact, I happen to work on such software. IMO it would be irresponsible to turn the code into an incomprehensible slop fest just because some AI boosters want me to believe that "coding is solved" or "software is dead".
1. At my company I am basically a regulatory requirement.
2. I’m paid to be a warm body who is available. That’s I am paid salaried instead of hourly.
3. I don’t really care if this situation changes in the future. If AI takes my job there’s nothing I can personally do to stop that from happening. I’m just saving my money and planning to retire as early as possible. I view work as a necessary evil.
Yes, but if I only use the excavator for 5 minutes and coast the remaining 55 minutes, I'm less useful than someone who uses the excavator the whole hour. Now, if I'm particularly skilled at using the excavator compared to my peers, I might get away with working less. If not, working 5 minutes while charging for 60 minutes will eventually get me fired.
Generally, automation does not make us work less because the expectations rise equivalently.
FWIW this is exactly how you negate a verb in German. For example: "Ich rauche nicht " -> "I don't smoke" or "Ich brauche keinen Mantel" -> "I don't need a coat"
English |-n't| is also a negation that applies to what comes before. In fact I don't think you actually can put a verbal negation before the verb in English in ordinary/modern circumstances; only a few verbs can be directly negated, and if one of those isn't present, one is added (usually "do", this is called "do-support") ... and the negation goes after it.
"This is how you negate a verb" -> "This is not how you negate a verb" ('is' can be negated, the negation goes after)
"This confuses the duke" -> "This does not confuse the duke" ('confuses' wants do-support to negate it, and the negation goes after 'do'.)
If you have structs that might need to be extended, you can use "versioned structs". One popular technique is to include the struct size as the first field. The struct size is set to `offsetof(mystruct, lastmember) + sizeof(lastmember)` and the receiver of the struct can use this to check if a member exists. The ARA API is a good example: https://github.com/Celemony/ARA_API/blob/65ec5c43b943a48cb54...
If you do version and size as the first two fields, you're basically there to backwards and forwards compatibility. Callers can request the size of the struct, allocate a buffer, and read out the parts they need. They can also check specific versions for what they support and ignore newer versions.
It also helps if you only append to the end of the struct for newer fields.
If you only access members, the version is not necessary. The receiver of the struct checks which members are present and ignores extra members that it does not know about. That's backward and forward compatibility.
The version could be useful if some members change their meaning across versions. But this would mean that you couldn't use a struct made from a future version because you wouldn't know about potentially changes.
Personally, I have only seen version or size, but not both. Do you have an example?
> It also helps if you only append to the end of the struct for newer fields.
This is actually required because changing or reordering existing members is an ABI break. Unless every version has its own dedicated struct. (This is often done in COM like APIs where interfaces are extended by adding a new interface. The client queries the must recent interface it knows about; if not supported, it tries the previous interface, and so on until it succeeds.)
`std::string_view` accepts both `const std::string&` and `const char*` without hidden allocations.
reply