I'm trying to compile WebKit with clang, and I'm hitting compile errors due to what is essentially the following pattern:
#include <iostream> #include <optional> struct X { X() = default; X(const X& other) { } }; struct Y { std::optional<X> x;; }; int main() { Y foo; Y bar(std::move(foo)); } So, they use std::optional<T> where T (in their case, WTF::Variant) has non-trivial copy/move constructors, and then use the std::optional move constructor. This compiles fine with GCC 8.1.1, but not with clang 6.0.1 (using GCC 8.1.1's libstdc++):
In file included from test.cpp:2: /bin/../lib64/gcc/x86_64-pc-linux-gnu/8.1.1/../../../../include/c++/8.1.1/optional:276:9: error: call to implicitly-deleted copy constructor of 'std::_Optional_payload<X, true, true, true>' : _Optional_payload(__engaged ^ ~~~~~~~~~ /bin/../lib64/gcc/x86_64-pc-linux-gnu/8.1.1/../../../../include/c++/8.1.1/optional:739:4: note: in instantiation of member function 'std::_Optional_payload<X, true, true, true>::_Optional_payload' requested here : _M_payload(__other._M_payload._M_engaged, ^ /bin/../lib64/gcc/x86_64-pc-linux-gnu/8.1.1/../../../../include/c++/8.1.1/optional:985:11: note: in instantiation of member function 'std::_Optional_base<X, false, false>::_Optional_base' requested here class optional ^ test.cpp:9:8: note: in implicit move constructor for 'std::optional<X>' first required here struct Y { ^ test.cpp:15:7: note: in implicit move constructor for 'Y' first required here Y bar(std::move(foo)); ^ /bin/../lib64/gcc/x86_64-pc-linux-gnu/8.1.1/../../../../include/c++/8.1.1/optional:288:24: note: copy constructor of '_Optional_payload<X, true, true, true>' is implicitly deleted because variant field '_M_payload' has a non-trivial copy constructor _Stored_type _M_payload; Is this valid C++, or is WebKit broken and clang rightfully rejects this code?
Y{}is already a temporary, sostd::moveis superfluous.