6 ms·
C++23: std:out_ptr and std:inout_ptr
- dataflow 4y agoI'm not sure how these are supposed to be helpful. You often won't know if a C API has deleted a pointer until you actually examine its return value. So instead of if (foo(p.release()) == 0) { ... } you generally need to do if (foo(p.get()) == 0) { p.release(); ... } Instead of reducing bugs, these might be introducing new footguns.
- plq 4y ago> Instead of reducing bugs, these might be introducing new footguns. Same thing could be/has been said about std::string_view or std::span but here we are. Like you said, a bare pointer could mean anything. This at least standardizes this particular footgun in question (which means one less reason to expose bare pointers in your API) so we are supposed to pay extra attention when we see one of those, I guess?
- dataflow 4y ago> Same thing could be/has been said about std::string_view or std::span but here we are. It's the first time I'm hearing this, and it doesn't sound like an opinion I've ever had. Those are just range-checked pointers; their ownership semantics aren't any different from those of raw pointers, and they're no less safe than raw pointers. In contrast these actually do have weird ownership semantics, and they have a propensity to introduce safety bugs into the common use cases that didn't exist before.
- abbeyj 4y ago> It's the first time I'm hearing this Say that you have some `Person` class with a `get_name()` method that returns the person's name. Then maybe you write some code like this to use it: Person person; // ... std::string_view name(person.get_name()); // Use `name` here Is this code safe? Well it depends on the implementation of `Person::get_name`. Maybe it looks like this: std::string& Person::get_name() { return m_name; } Then the code should be OK because the call will return a reference to a member of `person` and `person` will outlive `name`. But what if instead the implementation looks like this: std::string Person::get_name() { return m_first_name + " " + m_last_name; } Now you're in trouble. A temporary string will be created and the string_view will refer to that string. That temporary gets destroyed at the end of the statement and then `name` is dangling. By the time you get to use `name` it is already broken. It is very easy to make this kind of mistake. Much easier than with pointers IMO since you usually have to explicitly take a pointer to something and you can't directly get a pointer to a temporary (`std::string* p = &person.get_name()` is an error if `Person::get_name` returns `std::string`). By contrast, getting a const reference to a temporary and then constructing a string_view out of it is not an error or even a warning. It is all done implicitly so there is no indication in the code that it is even happening. The only difference between code that is correct and code that is incorrect is a single ampersand far away in some header file. This type of mistake probably won't get caught in code review. How often does a person really go track down and examine a header file when reviewing a pull request? Probably they're only going to look at the actual files that are being changed. Even worse, say that `Person::get_name` is using the first implementation so the code is correct and working. But later on somebody does a refactor such `Person::get_name` uses the second implementation. Now they've broken things but they're unaware of this fact. They want to be responsible and find any problems that they might have introduced. The first step is to run a build and see if there are any complaints from the compiler. That is successful and the compiler produces no warnings. Encouraged by this they then run all the tests. And all the tests pass! Reading memory just after it has been freed will often give you back the last thing that was stored there so the tests see the values that they expect. So with a successful build, a successful run of all the tests, and no problems spotted in code review, they confidently merge the change. And now you've got a use-after-free in your code that will eventually cause a problem. Yes, using Address Sanitizer will catch this. But it is still a pretty easy way to introduce a bug into your program. Code that does not look at all suspicious can be completely broken.
- blub 4y agoI would flag assigning a return value to a string_view in code review. Same as assigning to a pointer. Yes, it’s one more thing nudging the programmer toward unsafe use, but this is C++ we’re talking about: only the strong survive.
- electrograv 4y agoThe root cause of the danger you illustrate here IMO is actually a fault in the way the `std::string` to `std::string_view` implicit conversion is designed, as opposed to something inherently wrong with the concept of view/span classes. In my opinion, `string_view` and `span` are otherwise wonderfully much-needed concepts (that arguably should have been built into the language itself, like Rust slices) that should never have been designed to allow implicitly creating views of temporaries. Disabling implicit conversion/construction from rvalue references is quite possible in C++17 and beyond, and very effectively prevents accidentally implicitly viewing temporaries just fine (I’ve done this myself in some enhanced span-like classes I’ve written), so I’m really not sure why string_view was designed this way. https://en.cppreference.com/w/cpp/string/basic_string/operator_basic_string_view https://en.cppreference.com/w/cpp/string/basic_string/operat...
- abbeyj 4y agoI think that would be safer. But then it would presumably disallow things like: void frob(std::string_view sv); std::string a = "a"; std::string b = "b"; frob(a + b); This is allowed today and does not have any problems that I'm aware of. You could work around this by changing the call to: frob(std::string_view(a + b)); But that feels cumbersome. I think the goal of std::string_view was to be useful as a parameter for a function so that you could pass in anything remotely string-like and it would implicitly convert and do what you meant. Requiring explicit conversions at the call sites would go against that goal. Another thing that was desired was the ability to take an existing function that takes a `const std::string&` and change it to instead take a `std::string_view` and not have to update any of the callers. If some callers that worked with the old function are going to need an update to work with the new function then it gets a lot harder to change the function. Or maybe impossible if you don't control all of the callers. I don't see how you can get both the ease-of-use and the safety short of reinventing something like Rust's lifetimes.
- flohofwoe 4y agoWhen I see a raw pointer in C or C++ code I know automatically that gotchas are involved and I need to be extra careful. When I see std::string_view, nothing tells me that this may lead to memory corruption because the underlying storage may go away at any time (and the compiler will happily let this happen). New C++ features simply should not create new memory corruption footguns in addition to the existing ones, embarrassments like iterator invalidation are already bad enough (and worse than typical C memory management issues, because in C such issues are usually 'in your face', while C++ hides them beneath layers upon layers of stdlib abstractions).
- dataflow 4y ago> When I see std::string_view, nothing tells me that this may lead to memory corruption because the underlying storage may go away at any time That's not true. The word "view" quite explicitly tells you that it's a view... into something else, whose existence is generally independent of you (just like in real life!). If anything, it's clearer than an asterisk. > (and the compiler will happily let this happen). Just like with pointers and iterators. There's no difference here. > When I see a raw pointer in C or C++ code I know automatically that gotchas are involved and I need to be extra careful. You "know" this for iterators too. There's no reason why you can't "know" it for views too. It's not like they're introducing a new ownership concept here - these are just bounded versions of iterators and pointers.
- 112233 4y agoAnd the way lambdas are implemented, that can be type-erased into function<> with no regard to capture-by-reference. C++ is a huge sinking ship...
- deleted 4y ago[deleted]
- abbeyj 4y agoYour C API here takes a `T*` but I think that the intended use for these new functions is with C APIs that take `T**`. Those are somewhat inconvenient to make interoperate with smart pointers. For example, in C you might have something like this: void c_api_that_returns_via_an_out_parameter(T** ret) { *ret = new T(); } T* p; c_api_that_returns_via_an_out_parameter(&p); Then you might try to change this to use a smart pointer as: std::unique_ptr<T> p; c_api_that_returns_via_an_out_parameter(&p); But this won't compile because you can't take the address of a `std::unique_ptr<T>` and then pass it where a `T**` is expected. So instead you need to do something like this: T* tmp = 0; c_api_that_returns_via_an_out_parameter(&tmp); std::unique_ptr<T> p(tmp); That can be a bit of a pain. These new functions serve as adapters to make this easier. They take in a `std::unique_ptr<T>` and provide a `T*` that you can pass to the C API: std::unique_ptr<T> p; c_api_that_returns_via_an_out_parameter(std::out_ptr(p)); No temporary needed. If your C API only ever passes and returns `T*` and never uses `T**` then I think that you don't need these new functions and you can continue to do things the current way. For instance, if you have a C API that allocates a new object and then returns a pointer to it then you can take this return value and immediately store it into your smart pointer: std::unique_ptr<T> p(c_api_that_returns_a_pointer()); And if you have a C API that takes a `T**` and then deallocates it: c_api_that_deallocates(p.release()); Although really your smart pointer should be doing this for you so you'd never need to do it explicitly. If you're ever manually calling `.release()` then that indicates a place where the smart pointer abstraction has broken down. You want to minimize or eliminate that. Note that in the examples using the new functions there are no explicit calls to `.release()` anywhere and this is a good thing. I can't recall ever seeing a C API that conditionally deallocates the pointer passed in. Do you have an example? Most C APIs that I'm familiar with have one function that allocates a new object and a separate function that unconditionally deallocates it, acting analogously to `malloc` and `free`.
- dataflow 4y ago> If you're ever manually calling `.release()` then that indicates a place where the smart pointer abstraction has broken down. You want to minimize or eliminate that. Yes, that's life when you're at the boundary with a C API. > I can't recall ever seeing a C API that conditionally deallocates the pointer passed in. Do you have an example? realloc()? Generally when you have an in-out parameter, it's doing more than just releasing resources, so there's a potential for failure. And in the failure case, the caller will often retain ownership of any pointers it passed. In fact, off the top of my head, I can only think of one function that is contrary to this, and that is DeferWindowPos() in Windows, which invalidates the input handle even on failure.
- deleted 4y ago[deleted]
- pronlover723 4y agoa similar concept came up here https://www.youtube.com/watch?v=ELeZAKCN4tY https://www.youtube.com/watch?v=ELeZAKCN4tY
- IAmLiterallyAB 4y agoWhile herb did a talk about `in` and `out` parameters, it is very different than this concept.
- peppermint_gum 4y agoThe difference is that Cpp2's in/out references are actually useful. With them, it would be feasible to completely ban uninitialized variables. BTW, P2723[1] tries to solve the same problem by zero-initializing everything by default. I'm not a fan of it. It's less elegant, it incurs potential performance cost and zero isn't always the right default. [1] - https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2723r0.html https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p27...
- dataflow 4y agoI don't understand that proposal. Compilers can already add (and already have added) a flag to auto-initialize variables (-ftrivial-auto-var-init?) if they want to... so why isn't that the end of the story? It's one thing to say "all variables must be auto-initialized" (which can doesn't need this proposal, as compilers can already do it), and I can understand that. But it's another thing entirely to say "programs should be able to depend on the auto-initialized value", which is what this proposal seems to be actually asking for. That's asking for an explicit language feature to be made implicit, and it's orthogonal to the security issue it's claiming to address. Why?
- pjmlp 4y agoBecause it isn't part of the language official semantics, and is compiler specific.
- 4y ago
- samsaga2 4y agoI think the lots of changes that is having c++ in every release is killing the language.
- fabiensanglard 4y agoIt is really hard to keep up. I often have to Google for definition when I read code. Inevitably someone uses the latest feature.
- pjmlp 4y agoSay those stuck in Java 8, C# 7, Python 2, C89, Ada83, Fortran77,.... It is a pain, but languages are software products as well, so they either get new features to keep going on each new release or they wither and fade away. Eventually some do still die of complexity, replaced by simpler languages, and the cycle reboots, see PL/I, Algol 60/68, ...
- dmitrygr 4y ago> or they wither and fade away C and assembly enter the chat
- 331c8c71 4y ago> C and assembly enter the chat Wouldn't you agree that the fraction of software written in C or asm has shrunk tremendously over the last decades?
- pjmlp 4y agoC23 and AVX512 greet them.
- colejohnson66 4y agoI wonder what the ratio of intrinsics to raw assembly is
- 4y ago
- unglaublich 4y agoProblem with C++ is that it's easy to add stuff, and super hard to get it out of the language again. Inevitably leads to bloat and an accumulation of wrong decisions.
- blub 4y agoYou’re obviously wrong, it’s very hard to add stuff. Both technically and from an ISO perspective.
- johannes1234321 4y agoStill a lot simpler than removing something.
- lovecg 4y agoArguably a lot of newer additions are there as an alternative to past bad decisions, and things do get deprecated and removed now and then.
- bluGill 4y agoC++ is not much larger than any other language. It doesn't fit together as nice as others, but it isn't really much larger either. Unless your code is trivial, you will be working with a project that is much larger than all of C++. That last is also why no language can afford to get rid of anything (see python 3 which took over a decade to catch on), someone is using the old thing in code they don't want to touch.
- myspeed 4y agoIts better to migrate to rust. Instead of copying & implementing these things from other languages.
- yodsanklai 4y agoOften not possible when working on existing C++ codebase. C++ is here to stay...
- usesqlreally 4y agoWhy would you migrate from a language full of :: and <> and {} which forces you to think about weird concepts about memory, ownership, sharing and pointers, to migrate to a very similar language? It's like stopping huffing paint to start huffing petrol.
- norman784 4y agoWhile I don't agree to migrate, I do agree that new projects need to find a safer alternative to C++, do we have a better mature alternative at this point in time other than Rust? I'm not aware of any other that is production ready. Also if your only complain in syntax then is just your taste, I would have considered if you have complains about issues with the language, one that I can think of is async/await that is a WIP.
- norman784 4y agoI agree that ideally everything should be written in safer alternatives, in most scenarios is not feasible to migrate from C++ to another language, because of the code base size. I think cppfront[0] is the way to go if you want to write a safer language on top of C++, you can watch the talk about it here[1]. [0] https://github.com/hsutter/cppfront https://github.com/hsutter/cppfront [1] https://www.youtube.com/watch?v=ELeZAKCN4tY https://www.youtube.com/watch?v=ELeZAKCN4tY
- bluGill 4y agoThere is a good reason I'm not migrating to rust: I have 15million lines of C++ that we have written over the years, for a cost of hundreds of millions of dollars (probably over a billion by now). There is no way we can migrate to rust for less than more hundreds of millions, there is also the loss of new features that our competition can add while we are doing the migration effort. I'm looking at how to fit rust into our project, but it isn't easy. I can report already that bad programmers in rust write worse code than great programmers in C++...
- DoingIsLearning 4y agoI have to work with C++ professionally one and off based on client requirements is there an _efficient_ resource that would allow me to keep up with all that has happened beyond C++14? At the rate the committee adds stuff it feels like it is a full time job just to keep up. Also is there any initiative within C++ to do a subset dialect with bowling alley guard rails? Something analog to the old C++ JFS standard, or something analog to dlang '-betterC' dialect flag?
- Unpadded3167 4y agoNot sure if it's really what you are looking for, but the C++ Core Guidelines (https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines) are what I check when I have to touch a C++ code-base. It precise which parts of C++ are to be used and which are to be avoided according the creators.
- Liquid_Fire 4y agoThis repo has a concise summary of new features, if that is the sort of thing you are looking for: https://github.com/AnthonyCalandra/modern-cpp-features https://github.com/AnthonyCalandra/modern-cpp-features
- gpderetta 4y agocppreference[1] is fairly minimalist, usually up to date and clearly marks which feature came with which standard. It is generally useful for everyday c++ coding. [1] https://en.cppreference.com/ https://en.cppreference.com/
- throwaway74829 4y agoFull disclosure: I don't work with C++ anymore. You don't have to keep up with all the stuff it adds. If you actually need a certain thing C++ has added -- that you're unaware of -- you'll naturally find it when you go looking for it. Otherwise, it's best to just stick with the feature-set that you know. KISS/YAGNI/if it ain't broke, don't fix it (but if it is broke, find a better way). Et other cliches.
- deleted 4y ago[deleted]
- tialaramex 4y agoThe construction of the opening paragraph is awkward and I think probably outright wrong? > This week, let’s continue exploring the new world of C++23. We are going to discuss two new standard library functions and their outputs (std::out_ptr, std::inout_ptr), two new standard library types (std::out_ptr_t, std::inout_ptr_t). IMNSHO the two lists in parentheses should each be next to the thing referring to them, like this: This week, let’s continue exploring the new world of C++23. We are going to discuss two new standard library functions (std::out_ptr, std::inout_ptr) and their outputs, two new standard library types (std::out_ptr_t, std::inout_ptr_t).
- inDigiNeous 4y agoYay more stuff to think about, while not having any seeming benefits ? I really wish some committee would just figure out a C++ for the 2020s kind of, "One way fits most" solution that removes most of the legacy features from the language (like maybe just warning if you are about to use legacy features or just preventing you to use some obscure way to iterate containers for example that was superseded already in 2011 by C++11 for example. Is there anything like this in the plans ?
- kllrnohj 4y agoCarbon or CppFront is what you're looking for. C++ itself won't ever remove legacy just like other languages never really remove legacy. Nobody wants to be the next Python3.
- _aavaa_ 4y agoIdk python3 seems to be doing pretty well
- henrydark 4y agoGuido van Rossum mentions the break between python2 and python3 a bit in the recent Lex Fridman, you might be interested in this. Many companies and many of us put in hundreds to thousands of hours moving from 2 to 3.
- _aavaa_ 4y agoAnd I agree that this was a real cost. Yet despite that, one time, cost, enough companies chose to update the code rather than migrate. And the language is now a very popular choice. I personally think that never removing legacy aspect of a language is a clear recipe for insane complexity.
- kllrnohj 4y agoIt took 14 years to go from Python 3 being released to Python 2.7 finally being officially sunset. A date that was pushed back multiple years because there was such a strong python 2 holdout. And Python 3 is the success story here. You could instead easily end up being Perl 6 instead.
- planede 4y agoI would say that calling these "C APIs" is a bit misleading, as they have to call `new` for these helper types to be useful. I would call these legacy C++ APIs, although I have recently seen new APIs written with this horribly awful `void foo(T* out_param_for_owning_ptr)` interface. Interfacing with these with a smart pointer without a helper type is kind of inconvenient, since then you need to do something like: T* raw_owning_ptr; foo(&raw_owning_ptr); std::unique_ptr<T> ptr(raw_owning_ptr); `out_ptr` and friends are a bit niche, just for interfacing with these kinds of APIs.
- linkdd 4y agoWhy would you have to call `new`? Why would `malloc()` not work here?
- leeter 4y agoIt would; you'd just have to supply a custom "free_deleter" in the definition of the unique_ptr that calls free instead of delete. I've used this for Windows System calls like GetAddrInfoExW which requires calling FreeAddrInfoEx when you're done. std::unique_ptr<ADDRINFOEXW, addrinfo_deleter> holder; ADDRINFOEXW hint = { .ai_family = AF_UNSPEC }; if (const auto result = GetAddrInfoExW( sHost , nullptr , NS_ALL , nullptr , &hint , std::out_ptr(holder) , nullptr , nullptr , nullptr , nullptr); result != ERROR_SUCCESS) { // print error here }
- linkdd 4y agoThis seems reasonable for working with C APIs.
- kllrnohj 4y agoOr globally replace the new and delete operators to call malloc & free, as they commonly already do such as in libstd++ and libc++ But having malloc/free cross a library boundary (that is library A malloc and wants the caller in a different library to call free) is also a code smell in general and potentially very problematic. So you'll be generally doing custom deleters regardless