https://godbolt.org/z/GEW6Y3376 template <class T> struct X { X() = default; X(const X&) { throw; } private: explicit X(const X&) requires (T::value) = default; }; struct S { static constexpr bool value = true; }; int main() { const X<S> x; X<S> x2 = x; } Clang compiles. GCC doesn't. Who's on the right side? I guess it's Clang, because the two copy constructor is simply `ignored` in GCC. Is it a bug, seriously?
<source>:15:15: error: no matching function for call to 'X<S>::X(const X<S>&)' 15 | X<S> x2 = x; | ^ • there are 2 candidates • candidate 1: 'constexpr X<T>::X() [with T = S]' <source>:4:3: 4 | X() = default; | ^ • candidate expects 0 arguments, 1 provided • candidate 2: 'constexpr X<T>::X(const X<T>&) requires T::value [with T = S]' (ignored) <source>:7:12: 7 | explicit X(const X&) requires (T::value) = default; | ^ Where is ``candidate 1.5: 'X<T>::X(const X<T>&)'`` ?
It isn't shown because the more constrained one replaces it. There is only one copy constructor, decided by evaluating the constraints. The unconstrained one is not an "eligible special member function" in the terminology of the standard. EDG and MSVC accept this too, but I don't understand why. GCC seems correct to me, the only eligible copy constructor is the private explicit one, and so that is chosen by overload resolution, but that's ill-formed because it's not accessible and is explicit.
(In reply to Jonathan Wakely from comment #2) > It isn't shown because the more constrained one replaces it. There is only > one copy constructor, decided by evaluating the constraints. The > unconstrained one is not an "eligible special member function" in the > terminology of the standard. > > EDG and MSVC accept this too, but I don't understand why. GCC seems correct > to me, the only eligible copy constructor is the private explicit one, and > so that is chosen by overload resolution, but that's ill-formed because it's > not accessible and is explicit. That's fine. A crucial bug of Clang, EDG, and MSVC, I believe. `explicit` doesn't change the overload resolution in any way. Access checking doesn't, either. Thanks for your explanation!
Please don't close a bug as FIXED if nothing was fixed. An invalid bug did not get "fixed".
Hmm, GCC assumes that non-eligible special member functions could never be chosen by overload resolution, but that is not the case here due to difference in "explicit". And [over] doesn't consider "eligible", so it looks like GCC is wrong. P0848 doesn't seem to consider differences in explicit, perhaps it should be included in [class.special] "kind" so that GCC's assumption is valid.
(In reply to Jason Merrill from comment #5) > Hmm, GCC assumes that non-eligible special member functions could never be > chosen by overload resolution, but that is not the case here due to > difference in "explicit". And [over] doesn't consider "eligible", so it > looks like GCC is wrong. > > P0848 doesn't seem to consider differences in explicit, perhaps it should be > included in [class.special] "kind" so that GCC's assumption is valid. Should this be submitted to CWG? I prefer that GCC is correct. My sentiment is that, if you add `static_assert(__is_trivially_copyable(X<S>));`, then you'll see both Clang and GCC pass the assertion. But Clang just let the overload resolution choose the non-trivial overload, that's too aggressive. And if you change `X<S> x2 = x` to `X<S> x2(x)`, then Clang doesn't compile: calling a private constructor.
(In reply to Jonathan Wakely from comment #2) > It isn't shown because the more constrained one replaces it. There is only > one copy constructor, decided by evaluating the constraints. The > unconstrained one is not an "eligible special member function" in the > terminology of the standard. (In reply to Jason Merrill from comment #5) > Hmm, GCC assumes that non-eligible special member functions could never be > chosen by overload resolution, but that is not the case here due to > difference in "explicit". And [over] doesn't consider "eligible", so it > looks like GCC is wrong. > > P0848 doesn't seem to consider differences in explicit, perhaps it should be > included in [class.special] "kind" so that GCC's assumption is valid. I'm surprised when seeing that the special member function is really special in GCC. This makes me doubt if `explicit`ness matters in overload resolution. :[ https://godbolt.org/z/zszfshv3P `` template <class T> struct X { X() = default; X(const X&) { throw; } operator int() const { throw; } private: explicit X(const X&) requires (T::value) = default; explicit operator int() const requires (T::value) { return 37; } }; struct S { static constexpr bool value = true; }; static_assert(__is_trivially_copyable(X<S>)); int main() { const X<S> x; { X<S> x2 = x; // Clang compiles. GCC doesn't. } { X<S> x2(x); // Both don't compile. } int i = x; // Both compile. (int) x; // Both don't compile. } ``
(In reply to songb432 from comment #6) > Should this be submitted to CWG? It has been
https://cplusplus.github.io/CWG/issues/3234.html
The trunk branch has been updated by Jason Merrill <jason@gcc.gnu.org>: https://gcc.gnu.org/g:d9f352582a0f3516509a2cd26d130f1dcfa8d048 commit r17-4944-gd9f352582a0f3516509a2cd26d130f1dcfa8d048 Author: Jason Merrill <jason@redhat.com> Date: Wed Oct 7 00:21:56 2026 -0400 c++: eligible vs explicit [PR127627] We assume that a non-"eligible" special member function cannot be selected by overload resolution, but that isn't the case here due to the difference in "explicit". So check for that when filtering in add_method. PR c++/127627 gcc/cp/ChangeLog: * class.cc (add_method): Don't remove a less-explicit function. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/explicit21.C: New test.