================ @@ -24,6 +44,103 @@ using enum E; #endif } +namespace cwg2627 { // cwg2627: 19 +#if __cplusplus >= 202002L +struct C { + long long i : 8; + friend auto operator<=>(C, C) = default; +}; + +void f() { + C x{1}, y{2}; + static_cast<void>(x <=> y); + static_cast<void>(x.i <=> y.i); +} + +template<typename T> +struct CDependent { + T i : 8; + friend auto operator<=>(CDependent, CDependent) = default; +}; + +template<typename T> +concept three_way_comparable = requires(T t) { { t <=> t }; }; +template<typename T> +concept bf_three_way_comparable = requires(T t) { { t.i <=> t.i }; }; +static_assert(three_way_comparable<CDependent<long long>>); +static_assert(bf_three_way_comparable<CDependent<long long>>); +#endif + +#if __cplusplus >= 201103L +template<int W> +struct D { + __int128 i : W; +}; + +template<int W> +std::int64_t f(D<W> d) { + return std::int64_t{ d.i }; // #cwg2627-f +} + +template std::int64_t f(D<63>); +template std::int64_t f(D<64>); +template std::int64_t f(D<65>); +// since-cxx11-error-re@#cwg2627-f {{non-constant-expression cannot be narrowed from type '__int128' to 'std::int64_t' (aka '{{.+}}') in initializer list}} +// since-cxx11-note@-2 {{in instantiation of function template specialization 'cwg2627::f<65>' requested here}} +// since-cxx11-note@#cwg2627-f {{insert an explicit cast to silence this issue}} + +template<typename Target, typename Source> +Target g(Source x) { + return Target{ x.i }; // #cwg2627-g +} + +template<typename T, int N> +struct E { + T i : N; +}; + +template std::int16_t g(E<int, 16>); +template std::int16_t g(E<unsigned, 15>); +template std::int16_t g(E<unsigned, 16>); +// since-cxx11-error-re@#cwg2627-g {{non-constant-expression cannot be narrowed from type 'unsigned int' to '{{.+}}' in initializer list}} +// since-cxx11-note-re@-2 {{in instantiation of function template specialization 'cwg2627::g<{{.+}}, cwg2627::E<unsigned int, 16>>' requested here}} +// since-cxx11-note@#cwg2627-g {{insert an explicit cast to silence this issue}} +template std::uint16_t g(E<unsigned, 16>); +template std::uint16_t g(E<int, 1>); +// since-cxx11-error-re@#cwg2627-g {{non-constant-expression cannot be narrowed from type 'int' to '{{.+}}' in initializer list}} +// since-cxx11-note-re@-2 {{in instantiation of function template specialization 'cwg2627::g<{{.+}}, cwg2627::E<int, 1>>' requested here}} +// since-cxx11-note@#cwg2627-g {{insert an explicit cast to silence this issue}} + +template bool g(E<unsigned, 1>); +template bool g(E<int, 1>); +// since-cxx11-error@#cwg2627-g {{non-constant-expression cannot be narrowed from type 'int' to 'bool' in initializer list}} +// since-cxx11-note@-2 {{in instantiation of function template specialization 'cwg2627::g<bool, cwg2627::E<int, 1>>' requested here}} +// since-cxx11-note@#cwg2627-g {{insert an explicit cast to silence this issue}} + +template<typename Target, typename Source> +constexpr decltype(Target{ std::declval<Source>().i }, false) is_narrowing(int) { return false; } +template<typename Target, typename Source> +constexpr bool is_narrowing(long) { return true; } + +static_assert(!is_narrowing<std::int16_t, E<int, 16>>(0), ""); +static_assert(!is_narrowing<std::int16_t, E<unsigned, 15>>(0), ""); +static_assert(is_narrowing<std::int16_t, E<unsigned, 16>>(0), ""); +static_assert(!is_narrowing<std::uint16_t, E<unsigned, 16>>(0), ""); +static_assert(is_narrowing<std::uint16_t, E<int, 1>>(0), ""); +static_assert(!is_narrowing<bool, E<unsigned, 1>>(0), ""); +static_assert(is_narrowing<bool, E<int, 1>>(0), ""); + +template<int N> +struct F { + signed int x : N; + decltype(std::int16_t{ x }) dependent_narrowing; + decltype(unsigned{ x }) always_narrowing; +// since-cxx11-error@-1 {{non-constant-expression cannot be narrowed from type 'int' to 'unsigned int' in initializer list}} +// since-cxx11-note@-2 {{insert an explicit cast to silence this issue}} +}; +#endif +} ---------------- Endilll wrote:
```suggestion } // namespace cwg2627 ``` https://github.com/llvm/llvm-project/pull/78112 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits