[clang] [clang] [SemaCXX] Implement CWG2627 Bit-fields and narrowing conversions (PR #78112)
Vlad Serebrennikov via cfe-commits
cfe-commits at lists.llvm.org
Thu May 30 11:16:20 PDT 2024
================
@@ -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}}
----------------
Endilll wrote:
That's unexpected.
I like `using int64_t = long long; static_assert(sizeof(int64_t) == 8)` more than regular expressions.
https://github.com/llvm/llvm-project/pull/78112
More information about the cfe-commits
mailing list