GCC Newspaper
NOVEMBER 30, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
c++

Correct overload resolution for 'explicit' special member functions

Fixes C++ overload resolution to correctly consider 'explicit' special member functions, preventing less-explicit ones from being wrongly selected.

GCC’s handling of special member function selection during overload resolution has been corrected to properly account for the ‘explicit’ specifier. The compiler previously assumed that non-‘eligible’ functions were not selectable, but this overlooked the nuances of ‘explicit’. The change in add_method now ensures that less-explicit functions are not removed when a more-explicit candidate exists, fixing PR127627 and ensuring correct overload resolution for explicit special member functions.

In Details

The add_method function in gcc/cp/class.cc has been modified to avoid removing a less-explicit special member function if a more-explicit one is available. The previous logic incorrectly filtered out functions that were not 'eligible' without considering the 'explicit' keyword's role in overload resolution. This change ensures that the overload resolution process correctly distinguishes between different levels of explicitness, resolving a bug where a less-explicit function might have been chosen over a more-explicit one, contrary to C++ rules.

For Context
special member function
In C++, functions that are implicitly declared or defined by the compiler, such as constructors, destructor, assignment operators, and conversion operators.
overload resolution
The process by which the C++ compiler selects the most appropriate function or operator to call when multiple functions or operators with the same name exist in the current scope.
explicit
A C++ keyword used to prevent a constructor or conversion operator from being implicitly invoked. Functions declared as explicit can only be called directly.
eligible
In the context of C++ overload resolution, a function is 'eligible' if it meets certain criteria allowing it to be considered for selection. This commit clarifies that 'explicit' affects eligibility.
PR127627
Problem Report 127627: A bug report filed against GCC detailing an issue with explicit special member functions that has been fixed by this commit.
Filed Under: c++overload-resolutioncompiler-frontend