https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100982

            Bug ID: 100982
           Summary: wrong constraint in std::optional::operator=
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: hewillk at gmail dot com
  Target Milestone: ---

There is a typo in optional#L818:

  template<typename _Up>
    enable_if_t<__and_v<__not_<is_same<_Tp, _Up>>,
                is_constructible<_Tp, const _Up&>,
                is_assignable<_Tp&, _Up>,
                __not_<__converts_from_optional<_Tp, _Up>>,
                __not_<__assigns_from_optional<_Tp, _Up>>>,
                optional&>
    operator=(const optional<_Up>& __u)

It should be is_assignable<_Tp&, const _Up&>.

https://godbolt.org/z/x7Gb9a5v9

#include <optional>

struct U {};

struct T {
  explicit T(const U&);
  T& operator=(const U&);
  T& operator=(U&&) = delete;
};

int main() {
  std::optional<U> opt1;
  std::optional<T> opt2;
  opt2 = opt1;
}

Reply via email to