https://gcc.gnu.org/bugzilla/show_bug.cgi?id=125514

            Bug ID: 125514
           Summary: [16/17 Regression] libstdc++: reference_wrapper
                    triggers -Wsfinae-incomplete for forward-declared type
                    in C++17
           Product: gcc
           Version: 16.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: studyingfather at outlook dot com
  Target Milestone: ---

The following C++17 testcase triggers -Wsfinae-incomplete with GCC 16 and
GCC trunk, but not with GCC 15.

Testcase:

#include <functional>

class incomplete;

struct owner
{
  std::reference_wrapper<const incomplete> ref;
};

class incomplete
{
};

int main()
{
  return 0;
}


Command line:

g++ -std=c++17 -O2 -Wall -Werror -c repro.cpp

Observed result with GCC 16.1.1:

repro.cpp:10:7: error: defining 'incomplete', which previously failed to be
complete in a SFINAE context [-Werror=sfinae-incomplete=]
In file included from /usr/include/c++/16.1.1/functional:74,
                 from repro.cpp:1:
/usr/include/c++/16.1.1/bits/refwrap.h:227:12: note: here.  Use
'-Wsfinae-incomplete=2' for a diagnostic at that point
  227 |     struct _Reference_wrapper_base
      |            ^~~~~~~~~~~~~~~~~~~~~~~
cc1plus: all warnings being treated as errors

Known to work:
- GCC 15.x

Known to fail:
- GCC 16.1.1 20260430
- GCC trunk / GCC 17 development snapshot, tested on Compiler Explorer on
  2026-05-30

Target:
- x86_64-pc-linux-gnu

I am filing this under libstdc++ because the SFINAE check that observes the
incomplete type appears to happen inside libstdc++'s C++17 implementation of
std::reference_wrapper, in bits/refwrap.h, rather than in user code.

I understand that std::reference_wrapper<T> was not specified to support
incomplete T before C++20, and that using incomplete types with
standard-library
templates before such support is explicitly specified may be outside the
guaranteed C++17 behavior. Therefore I am not claiming this is necessarily
valid C++17 code that GCC must accept.

However, this is a diagnostic/build compatibility regression for existing
C++17 code. The user code only stores a std::reference_wrapper<const T> to a
forward-declared type. The warning is emitted because libstdc++'s internal
C++17 reference_wrapper compatibility machinery probes legacy callable nested
typedefs such as argument_type / first_argument_type / second_argument_type.
With -Werror this turns code that built with GCC 15 into a build failure with
GCC 16 and trunk.

Could this warning be avoided or suppressed for this libstdc++ implementation
detail, or is this diagnostic intentional for std::reference_wrapper<T> with
incomplete T in C++17?

If this is considered a C++ front-end diagnostic issue rather than a libstdc++
issue, please reassign.

Reply via email to