• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

steve-downey / optional
100%
main: 99%

Build:
Build:
LAST BUILD BRANCH: dependabot/pip/gcovr-8.5
DEFAULT BRANCH: main
Repo Added 24 Jan 2025 03:22AM UTC
Token fe3OLjedhBqeSGvTyfsohUn7OMCZjmDXl regen
Build 558 Last
Files 10
Badge
Embed ▾
README BADGES
x

If you need to use a raster PNG badge, change the '.svg' to '.png' in the link

Markdown

Textile

RDoc

HTML

Rst

LAST BUILD ON BRANCH P2988R12_misc_fixes
branch: P2988R12_misc_fixes
CHANGE BRANCH
x
Reset
Sync Branches
  • P2988R12_misc_fixes
  • LWG
  • LWG-20250314
  • LWG4072-constrain-compare
  • LWG4406-value_or
  • beman-tidy
  • chore/GHA-221908-stepsecurity-remediation
  • ci-fix-20251230
  • clang21
  • cmake-workflow
  • constexpr-20
  • dependabot-config
  • dependabot/github_actions/github-actions-81e7014515
  • dependabot/github_actions/github-actions-b58be05293
  • dependabot/github_actions/github-owned-actions-08b3065b60
  • dependabot/github_actions/github-owned-actions-ebdf822f7d
  • dependabot/pip/clang-format-21.1.8
  • dependabot/pip/gcovr-8.5
  • deps-20260104
  • docu
  • docusaurus
  • doxify
  • feature_test
  • find_package
  • fix-ci-gcov
  • fixdocs
  • format_kind
  • infra
  • init_vars
  • installtest
  • lsp-work
  • lwg4304
  • main
  • makefile-fix
  • more_coverage
  • mrdocs
  • msan
  • optimize_adaptors
  • or-else-constraint
  • rebase_optimize_adaptors
  • rel1prep
  • update-precommit

01 Jul 2025 10:57PM UTC coverage: 100.0%. First build
16595319667

push

github

dangelog
optional<T&>::or_else: copy *this instead of creating a new optional

If or_else is called on an engaged optional, we're supposed to return a
copy (or a move) of the `*this` object; otherwise we invoke the argument
of or_else, and return whatever optional that returns.

For optional<T> we were doing exactly that (`return *this` or
`move(*this)`). For optional<T&> we were instead doing `return *value_`,
where `value_` is the pointer used in the implementation. That ends up
creating an optional<T&> through its "value constructor".

The problem is that the two forms are not equivalent in corner cases;
consider this code:

```
T *obj = new T;
T &ref = *obj;
delete obj;     // obj now dangles

T &ref2 = ref;  // OK
```

The last line is OK even if `ref` does not refer to an object any more.

This code is instead not OK:

```
T *obj = new T;
T &ref = *obj;
delete obj;

T &ref2 = *obj; // UB, https://eel.is/c++draft/expr.unary.op#1
```

If we use optional<T&>::or_else, the implementation is isomorphic to the
second form, not to the first one:

```
T *obj = new T;
optional<T &> ref = *obj;
delete obj;

assert(ref); // OK
optional<T &> ref2 = ref.or_else(/* ... */); // UB; does *obj internally
```

We can avoid this UB by avoiding the dereference into
optional<T&>::or_else, and returning a copy of *this instead. The
semantics are otherwise the same, but we avoid tripping into UB.

I'm adding a test which however is inconclusive because compilers do
not detect the above UB during constant evaluation, although they're
required to do so. That's likely a bug.

1 of 1 new or added line in 1 file covered. (100.0%)

309 of 309 relevant lines covered (100.0%)

118790.4 hits per line

Relevant lines Covered
Build:
Build:
309 RELEVANT LINES 309 COVERED LINES
118790.4 HITS PER LINE
Source Files on P2988R12_misc_fixes
  • Tree
  • List 4
  • Changed 0
  • Source Changed 0
  • Coverage Changed 0
Coverage ∆ File Lines Relevant Covered Missed Hits/Line

Recent builds

Builds Branch Commit Type Ran Committer Via Coverage
16595319667 P2988R12_misc_fixes optional<T&>::or_else: copy *this instead of creating a new optional If or_else is called on an engaged optional, we're supposed to return a copy (or a move) of the `*this` object; otherwise we invoke the argument of or_else, and return whatever ... push 29 Jul 2025 11:56AM UTC dangelog github
100.0
See All Builds (517)

Badge your Repo: optional

We detected this repo isn’t badged! Grab the embed code to the right, add it to your repo to show off your code coverage, and when the badge is live hit the refresh button to remove this message.

Could not find badge in README.

Embed ▾
README BADGES
x

If you need to use a raster PNG badge, change the '.svg' to '.png' in the link

Markdown

Textile

RDoc

HTML

Rst

Refresh
  • Settings
  • Repo on GitHub
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc