https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98620
Bug ID: 98620
Summary: SFINAE code in class specialization generate warnings
Product: gcc
Version: 11.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: m.cencora at gmail dot com
Target Milestone: ---
Following code compiled in any gcc>=5, C++11 or higher, and
-Wmissing-field-initializers generate compiler warning:
g++ -std=c++20 -Wmissing-field-initializers
#include <type_traits>
template <typename T>
struct TmpArray
{
T arr[1];
};
template <typename Src, typename Dst, typename = void>
struct is_non_narrowing_conversion : std::false_type
{};
template <typename Src, typename Dst>
struct is_non_narrowing_conversion<
Src, Dst,
decltype(void(TmpArray<Dst>{{ std::declval<Src>() }}))
> : std::true_type
{};
struct mystruct
{
int a;
void * b;
};
void test_nok()
{
is_non_narrowing_conversion<int&, mystruct>::type v;
}
SFINAE is used here only to detect if a code compiles, and code inside the
decltype should not generate warnings.
Also this is inconsistent, because when SFINAE is used in function template the
warning is not generated:
template <typename Dst, typename Src>
auto is_non_narrowing_conversion_func_impl(Src&&, int)
-> decltype((TmpArray<Dst>{{ std::declval<Src>() }}, std::true_type{}));
template <typename Dst, typename Src>
auto is_non_narrowing_conversion_func_impl(Src&&, ...)
-> std::false_type;
template <typename Src, typename Dst>
using is_non_narrowing_conversion_v2 =
decltype(is_non_narrowing_conversion_func_impl<Dst>(std::declval<Src>(), 0));
void test_ok()
{
is_non_narrowing_conversion_v2<int&, mystruct> v;
}