[all-commits] [llvm/llvm-project] df78ca: The standard does not require `optional<T>(U&&)` t...

Yuxuan Chen via All-commits all-commits at lists.llvm.org
Wed Jun 10 16:11:35 PDT 2026


  Branch: refs/heads/users/yuxuanchen1997/libcxx-optional-value-ctor-noexcept
  Home:   https://github.com/llvm/llvm-project
  Commit: df78caf18faf928f1d3da2e912f11317cd353911
      https://github.com/llvm/llvm-project/commit/df78caf18faf928f1d3da2e912f11317cd353911
  Author: Yuxuan Chen <ych at meta.com>
  Date:   2026-06-10 (Wed, 10 Jun 2026)

  Changed paths:
    M libcxx/include/optional
    M libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/U.pass.cpp

  Log Message:
  -----------
  The standard does not require `optional<T>(U&&)` to be potentially throwing; it simply does not specify noexcept for the primary `optional<T>` converting constructor. Standard library implementations are permitted [[res.on.exception.handling]/5](https://eel.is/c++draft/res.on.exception.handling#5) to strengthen exception specifications for non-virtual library functions, as long as the strengthened specification is correct.

GNU libstdc++ already does this:
https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/std/optional#L911-L913
https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/std/optional#L962-L974

The existing libc++ code only added noexcept for the C++26 `optional<T&>` case, guarded by `is_lvalue_reference_v<_Tp>`. It is safe to remove that gate and use the general condition instead:
```
noexcept(is_nothrow_constructible_v<_Tp, _Up>)
```

For `optional<T&>`, this still becomes the intended reference-construction check. For ordinary `optional<T>`, it correctly reflects whether constructing `T` from `U` can throw. The constructor only forwards into the contained object construction and updates optional bookkeeping, so if `T` is nothrow-constructible from `U`, the optional construction is also nothrow.



To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications


More information about the All-commits mailing list