c++: Add test for C++23 narrowing conv to bool
authorJason Merrill <jason@redhat.com>
Tue, 8 Jun 2021 18:24:16 +0000 (14:24 -0400)
committerJason Merrill <jason@redhat.com>
Tue, 8 Jun 2021 19:34:42 +0000 (15:34 -0400)
From wg21.link/P1401R5.

gcc/testsuite/ChangeLog:

* g++.dg/cpp23/narrowing-bool1.C: New test.

gcc/testsuite/g++.dg/cpp23/narrowing-bool1.C [new file with mode: 0644]

diff --git a/gcc/testsuite/g++.dg/cpp23/narrowing-bool1.C b/gcc/testsuite/g++.dg/cpp23/narrowing-bool1.C
new file mode 100644 (file)
index 0000000..54906c9
--- /dev/null
@@ -0,0 +1,22 @@
+// P1401R5: Narrowing contextual conversions to bool
+// { dg-do compile { target c++11 } }
+
+void f() noexcept(sizeof(char[2])); // { dg-error "narrowing" } conversion of value 2 to type bool
+void g() noexcept(sizeof(char)); // OK, conversion of value 1 to type bool is non-narrowing
+
+#if __cpp_conditional_explicit
+struct S {
+  explicit(sizeof(char[2])) S(char); // { dg-error "narrowing" "" { target c++20 } }
+  explicit(sizeof(char)) S(bool); // OK, conversion of value 1 to type bool is non-narrowing
+};
+#endif
+
+static_assert(sizeof(int[2]), ""); // OK, narrowing allowed
+
+#if __cpp_if_constexpr
+int main()
+{
+  if constexpr (sizeof(int[2])) // OK, narrowing allowed
+    {}
+}
+#endif