From 8483dd99268f4d2b68521fcefa78fbdad21f21e8 Mon Sep 17 00:00:00 2001 From: Jason Merrill Date: Tue, 8 Jun 2021 14:24:16 -0400 Subject: [PATCH] c++: Add test for C++23 narrowing conv to bool From wg21.link/P1401R5. gcc/testsuite/ChangeLog: * g++.dg/cpp23/narrowing-bool1.C: New test. --- gcc/testsuite/g++.dg/cpp23/narrowing-bool1.C | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 gcc/testsuite/g++.dg/cpp23/narrowing-bool1.C diff --git a/gcc/testsuite/g++.dg/cpp23/narrowing-bool1.C b/gcc/testsuite/g++.dg/cpp23/narrowing-bool1.C new file mode 100644 index 0000000..54906c9 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp23/narrowing-bool1.C @@ -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 -- 2.7.4