Issue 141648
Summary [libc++] std::variant invokes move constructor of non-movable alternative in copy assignment
Labels libc++
Assignees
Reporter whess
    Link to godbolt example: https://godbolt.org/z/r4as6sejq

```
struct CopyOnly {
    explicit CopyOnly(const std::string& str) {}
    CopyOnly(const CopyOnly&) = default;
    CopyOnly(CopyOnly&&) = delete;
    CopyOnly& operator=(const CopyOnly&) = default;
    CopyOnly& operator=(CopyOnly&&) = delete;

    std::string str;
};

int main() {
 std::variant<std::string, CopyOnly> v;
    std::variant<std::string, CopyOnly> v2;
    v2 = v; // Compile error

    return 0;
}
```

Compilation error:

```
In file included from <source>:1:
/opt/compiler-explorer/clang-14.0.0/bin/../include/c++/v1/variant:709:9: error: call to deleted constructor of 'std::__variant_detail::__alt<1, CopyOnly>::__value_type' (aka 'CopyOnly')
      : __value(_VSTD::forward<_Args>(__args)...) {}
        ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/opt/compiler-explorer/clang-14.0.0/bin/../include/c++/v1/variant:877:9: note: in instantiation of function template specialization 'std::__variant_detail::__alt<1, CopyOnly>::__alt<CopyOnly>' requested here
 __alt<_Ip, _Tp>(in_place, _VSTD::forward<_Args>(__args)...);
 ^
/opt/compiler-explorer/clang-14.0.0/bin/../include/c++/v1/variant:990:25: note: in instantiation of function template specialization 'std::__variant_detail::__ctor<std::__variant_detail::__traits<std::string, CopyOnly>>::__construct_alt<1UL, CopyOnly, CopyOnly>' requested here
 auto& __res = this->__construct_alt(__access::__base::__get_alt<_Ip>(*this),
 ^
/opt/compiler-explorer/clang-14.0.0/bin/../include/c++/v1/variant:1003:7: note: in instantiation of member function 'std::__variant_detail::__assignment<std::__variant_detail::__traits<std::string, CopyOnly>>::__assign_alt(__alt<1UL, CopyOnly> &, const CopyOnly &)::(anonymous struct)::operator()' requested here
      struct {
 ^
/opt/compiler-explorer/clang-14.0.0/bin/../include/c++/v1/variant:1029:19: note: in instantiation of function template specialization 'std::__variant_detail::__assignment<std::__variant_detail::__traits<std::string, CopyOnly>>::__assign_alt<1UL, CopyOnly, const CopyOnly &>' requested here
 this->__assign_alt(
 ^
/opt/compiler-explorer/clang-14.0.0/bin/../include/c++/v1/variant:1108:13: note: in instantiation of function template specialization 'std::__variant_detail::__assignment<std::__variant_detail::__traits<std::string, CopyOnly>>::__generic_assign<const std::__variant_detail::__copy_assignment<std::__variant_detail::__traits<std::string, CopyOnly>, std::__variant_detail::_Trait::_Available> &>' requested here
 this->__generic_assign(__that);
 ^
/opt/compiler-explorer/clang-14.0.0/bin/../include/c++/v1/variant:1119:28: note: in instantiation of member function 'std::__variant_detail::__copy_assignment<std::__variant_detail::__traits<std::string, CopyOnly>, std::__variant_detail::_Trait::_Available>::operator=' requested here
class _LIBCPP_TEMPLATE_VIS __impl
 ^
<source>:8:5: note: 'CopyOnly' has been explicitly marked deleted here
 CopyOnly(CopyOnly&&) = delete;
    ^
1 error generated.
Compiler returned: 1
```

Compilation error does not occur in libstdc++. I'd expect this to compile without issue, invoking the copy constructor/assignment of the non-movable alternative where needed.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to