isocpp / isocpp/CppCoreGuidelines
Semantics of not-null with a move-only pointer type
Open
@gdr-at-ms is already working on this.
Since Nov 5, 2015.
open
- Dominant language
- CSS
- Stars
- 45.3k
- Forks
- 5.6k
- PR merge metrics
- No merged PRs in 30d
Description
From Matt Austern, capturing here:
What is the intended behavior for a moved-from not_null containing a unique_ptr. I can think of a number of possible answers, all slightly unfortunate.
Here are two options in C++14:
- Say that not_null should have copy operations (necessary) but explicitly delete move operations (also I think necessary as they make no sense). Then a not_null of a move_only type is neither copyable nor movable. Unfortunately, this means you can't return a
not_null<unique_ptr<T>>by value. If we get the concept of a destructive move in the standard in C++17 or later, then we should be able to apply that in this case and allow destructive move for a not_null, which would re-enable pass and return by value. - Say that not_null fails/throws when null, noting that it's supposed to be a special case hard to get into. One argument for this version is that this situation feels quite a bit like the central example in the
variantdiscussion and that's where we ended up forvariant. Unfortunately, this injects null tests on deref and it's not clear whether the special case is as special as it is forvariant.
As Matt notes, both are slightly unfortunate -- (1) for usability, and (2) for requiring more work in the optimizer.
More ideas welcome!
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.