Skip to content

can't construct core::Optional from rvalue with deleted copy constructor #43

@dnsge

Description

@dnsge

Minimum example:

class X {
public:
    X(int);

    static Optional<X> Make();

    X(const X&) = delete;
    X& operator=(const X&) = delete;
    X(X&&) noexcept = default;
    X& operator=(X&&) noexcept = default;
};

Optional<X> X::Make() {
    return X{1};
}

Compilation error:

In file included from /Users/dnsge/dev/class/eecs498.5/search/lib/tests/optional_test.cpp:1:
/Users/dnsge/dev/class/eecs498.5/search/lib/include/core/optional.h:242:9: error: no matching function for call to 'construct_at'
  242 |         std::construct_at(&val_, std::forward<Args>(args)...);
      |         ^~~~~~~~~~~~~~~~~
/Users/dnsge/dev/class/eecs498.5/search/lib/include/core/optional.h:87:9: note: in instantiation of function template specialization 'core::Optional<X>::DirectInitVal<X &>' requested here
   87 |         DirectInitVal(val);
      |         ^
/Users/dnsge/dev/class/eecs498.5/search/lib/tests/optional_test.cpp:23:12: note: in instantiation of function template specialization 'core::Optional<X>::Optional<X>' requested here
   23 |     return X{1};
      |            ^
/opt/homebrew/Cellar/llvm/19.1.7/bin/../include/c++/v1/__memory/construct_at.h:39:38: note: candidate template ignored: substitution failure [with _Tp = X, _Args = <X &>]: call to deleted constructor of 'X'
   38 | template <class _Tp, class... _Args, class = decltype(::new(std::declval<void*>()) _Tp(std::declval<_Args>()...))>
      |                                                                                    ~~~
   39 | _LIBCPP_HIDE_FROM_ABI constexpr _Tp* construct_at(_Tp* __location, _Args&&... __args) {
      |                                      ^
1 error generated.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions