isocpp / isocpp/CppCoreGuidelines

Semantics of not-null with a move-only pointer type

Open
#379 7 comments 0 reactions 1 assignee View on GitHub

@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:

  1. 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.
  2. 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 variant discussion and that's where we ended up for variant. Unfortunately, this injects null tests on deref and it's not clear whether the special case is as special as it is for variant.

As Matt notes, both are slightly unfortunate -- (1) for usability, and (2) for requiring more work in the optimizer.

More ideas welcome!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.