Bug 127627 - What's the right overload resolution for ``public: X(const X&); private: explicit X(const X&) requires T::value = default;``?
Summary: What's the right overload resolution for ``public: X(const X&); private: expl...
Status: ASSIGNED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 16.2.0
: P3 normal
Target Milestone: ---
Assignee: Jason Merrill
URL:
Keywords:
Depends on:
Blocks: concepts
  Show dependency tree / graph
 
Reported: 2026-09-26 16:26 UTC by songb432
Modified: 2026-10-07 04:22 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2026-09-30 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description songb432 2026-09-26 16:26:47 UTC
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?
Comment 1 songb432 2026-09-26 16:29:34 UTC
<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>&)'`` ?
Comment 2 Jonathan Wakely 2026-09-28 11:07:45 UTC
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.
Comment 3 songb432 2026-09-28 18:01:15 UTC
(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!
Comment 4 Jonathan Wakely 2026-09-30 11:49:34 UTC
Please don't close a bug as FIXED if nothing was fixed. An invalid bug did not get "fixed".
Comment 5 Jason Merrill 2026-09-30 16:41:06 UTC
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.
Comment 6 songb432 2026-10-01 07:38:22 UTC
(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.
Comment 7 songb432 2026-10-01 07:46:27 UTC
(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.
}
``
Comment 8 Jonathan Wakely 2026-10-01 08:36:26 UTC
(In reply to songb432 from comment #6)
> Should this be submitted to CWG?

It has been
Comment 9 Jakub Jelinek 2026-10-01 09:36:11 UTC
https://cplusplus.github.io/CWG/issues/3234.html
Comment 10 GCC Commits 2026-10-07 04:22:05 UTC
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.