https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92570
Bug ID: 92570 Summary: clang fails to instantiate std::optional<A> if A is not const copy constructible Product: gcc Version: 10.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: libstdc++ Assignee: unassigned at gcc dot gnu.org Reporter: romain.geissler at amadeus dot com Target Milestone: --- Hi, The following snippet fails to build with clang >= 8 using libstdc++ >= 8.1.0: #include <optional> class A { public: A() = default; A(A&) = default; }; void f() { std::optional<A> a1 [[maybe_unused]]; //std::optional<A> a2(a1); } compiled with -std=gnu++17. gcc is ok with that if we keep commented the last line of "f". However clang seems to eagerly check if " = default" declaration actually make sense, even if it's never used. It reads: In file included from <source>:1: /opt/compiler-explorer/gcc-9.2.0/lib/gcc/x86_64-linux-gnu/9.2.0/../../../../include/c++/9.2.0/optional:148:7: error: the parameter for this explicitly-defaulted copy constructor is const, but a member or base requires it to be non-const _Optional_payload_base(const _Optional_payload_base&) = default; ^ /opt/compiler-explorer/gcc-9.2.0/lib/gcc/x86_64-linux-gnu/9.2.0/../../../../include/c++/9.2.0/optional:354:7: note: in instantiation of template class 'std::_Optional_payload_base<A>' requested here : _Optional_payload_base<_Tp> ^ /opt/compiler-explorer/gcc-9.2.0/lib/gcc/x86_64-linux-gnu/9.2.0/../../../../include/c++/9.2.0/optional:511:30: note: in instantiation of template class 'std::_Optional_payload<A, true, false, false>' requested here _Optional_payload<_Tp> _M_payload; ^ /opt/compiler-explorer/gcc-9.2.0/lib/gcc/x86_64-linux-gnu/9.2.0/../../../../include/c++/9.2.0/optional:657:15: note: in instantiation of template class 'std::_Optional_base<A, false, false>' requested here : private _Optional_base<_Tp>, ^ <source>:12:22: note: in instantiation of template class 'std::optional<A>' requested here std::optional<A> a1 [[maybe_unused]]; ^ 1 error generated. Compiler returned: 1 Here I am not sure who is really right. Shall libstdc++ extensively use things like enable_if to conditionally define all these explicitly defaulted functions to please clang, or is clang too eager in checking this even when it's not actually used/needed ? I am fine opening a clang bug report if you consider the issue is on clang side. Cheers, Romain