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

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Shuhao Zhang from comment #0)
> The following C++17 testcase triggers -Wsfinae-incomplete with GCC 16 and
> GCC trunk, but not with GCC 15.

Because that warning didn't exist in GCC 15.
Your program still had the problem, GCC just didn't diagnose it.

> 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.

As required by the C++17 standard.

> With -Werror this turns code that built with GCC 15 into a build failure with
> GCC 16 and trunk.

Don't use -Werror like that then.

> Could this warning be avoided or suppressed for this libstdc++ implementation
> detail,

It's not an implementation detail. As Tomasz said, it's a requirement of the
C++17 standard. std::reference_wrapper has a "weak result type" which means we
need to probe the wrapped type and then conditionally define the nested
std::reference_wrapper::result_type. Similarly for argument_type,
first_argument_type, and second_argument_type.

> or is this diagnostic intentional for std::reference_wrapper<T> with
> incomplete T in C++17?

If you want the warning to be avoided, use -Wno-sfinae-incomplete or
-Wno-error=sfinae-incomplete.

You can also suppress the warning by putting diagnostic pragmas around the
definition that makes the class complete:

class incomplete;

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

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsfinae-incomplete"
class incomplete
{
};
#pragma GCC diagnostic pop

Reply via email to