David Seifert wrote:

attached the patch. I believe this patch in fact increases strictness
even for C code, as it relies on the proper functioning of the "bool"
macro. I have tested it with multiple C and C++ compilers on OS X and
Linux:

Thanks, and sorry about the late reply. I installed the attached patch into Gnulib; I think it should solve the problem in a somewhat-different way.
>From f2f0962f49f89ccb2463b3336cae2ad271954047 Mon Sep 17 00:00:00 2001
From: Paul Eggert <egg...@cs.ucla.edu>
Date: Wed, 17 Aug 2016 15:33:06 -0700
Subject: [PATCH] stdbool: don't require _Bool for C++

Problem reported by David Seifert in:
http://lists.gnu.org/archive/html/bug-gnulib/2016-06/msg00005.html
* NEWS, doc/posix-headers/stdbool.texi (stdbool.h): Document this.
* m4/stdbool.m4 (AC_CHECK_HEADER_STDBOOL): Make the check
more-forgiving for C++, in that it requires only 'bool'.  Be a bit
stricter about checking that bool and _Bool are compatible in C.
---
 ChangeLog                      | 10 ++++++++++
 NEWS                           |  4 ++++
 doc/posix-headers/stdbool.texi |  3 ++-
 m4/stdbool.m4                  | 28 ++++++++++++++++------------
 4 files changed, 32 insertions(+), 13 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index e899eb4..ddacf1e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2016-08-17  Paul Eggert  <egg...@cs.ucla.edu>
+
+	stdbool: don't require _Bool for C++
+	Problem reported by David Seifert in:
+	http://lists.gnu.org/archive/html/bug-gnulib/2016-06/msg00005.html
+	* NEWS, doc/posix-headers/stdbool.texi (stdbool.h): Document this.
+	* m4/stdbool.m4 (AC_CHECK_HEADER_STDBOOL): Make the check
+	more-forgiving for C++, in that it requires only 'bool'.  Be a bit
+	stricter about checking that bool and _Bool are compatible in C.
+
 2016-08-16  Paul Eggert  <egg...@cs.ucla.edu>
 
 	getdelim: remove dependency on realloc-posix
diff --git a/NEWS b/NEWS
index 00709e8..e33d350 100644
--- a/NEWS
+++ b/NEWS
@@ -42,6 +42,10 @@ User visible incompatible changes
 
 Date        Modules         Changes
 
+2016-08-17  stdbool         This no longer supports _Bool for C++.
+                            Programs intended to be portable to C++
+                            compilers should use plain 'bool' instead.
+
 2016-04-12  intprops        The following macros were removed:
                             TYPE_TWOS_COMPLEMENT  TYPE_ONES_COMPLEMENT
                             TYPE_SIGNED_MAGNITUDE
diff --git a/doc/posix-headers/stdbool.texi b/doc/posix-headers/stdbool.texi
index 50afbd4..bac30aa 100644
--- a/doc/posix-headers/stdbool.texi
+++ b/doc/posix-headers/stdbool.texi
@@ -20,7 +20,8 @@ OpenBSD 4.7 with gcc 2.95.
 Portability problems not fixed by Gnulib:
 @itemize
 @item
-@code{<stdbool.h>} must be #included before @samp{_Bool} can be used.
+@samp{_Bool} cannot be used before @code{<stdbool.h>} is included, or if
+the program is intended to be compiled by a C++ compiler.
 @item
 You cannot assume that @code{_Bool} is a typedef; it might be a macro.
 @item
diff --git a/m4/stdbool.m4 b/m4/stdbool.m4
index a556153..2a9b1db 100644
--- a/m4/stdbool.m4
+++ b/m4/stdbool.m4
@@ -5,7 +5,7 @@ dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
 dnl with or without modifications, as long as this notice is preserved.
 
-#serial 6
+#serial 7
 
 # Prepare for substituting <stdbool.h> if it is not supported.
 
@@ -44,7 +44,10 @@ AC_DEFUN([AC_CHECK_HEADER_STDBOOL],
            [[
              #include <stdbool.h>
 
-             #if __cplusplus < 201103
+             #ifdef __cplusplus
+              typedef bool Bool;
+             #else
+              typedef _Bool Bool;
               #ifndef bool
                "error: bool is not defined"
               #endif
@@ -66,37 +69,38 @@ AC_DEFUN([AC_CHECK_HEADER_STDBOOL],
               "error: __bool_true_false_are_defined is not defined"
              #endif
 
-             struct s { _Bool s: 1; _Bool t; } s;
+             struct s { Bool s: 1; Bool t; bool u: 1; bool v; } s;
 
              char a[true == 1 ? 1 : -1];
              char b[false == 0 ? 1 : -1];
              char c[__bool_true_false_are_defined == 1 ? 1 : -1];
              char d[(bool) 0.5 == true ? 1 : -1];
              /* See body of main program for 'e'.  */
-             char f[(_Bool) 0.0 == false ? 1 : -1];
+             char f[(Bool) 0.0 == false ? 1 : -1];
              char g[true];
-             char h[sizeof (_Bool)];
+             char h[sizeof (Bool)];
              char i[sizeof s.t];
              enum { j = false, k = true, l = false * true, m = true * 256 };
              /* The following fails for
                 HP aC++/ANSI C B3910B A.05.55 [Dec 04 2003]. */
-             _Bool n[m];
+             Bool n[m];
              char o[sizeof n == m * sizeof n[0] ? 1 : -1];
-             char p[-1 - (_Bool) 0 < 0 && -1 - (bool) 0 < 0 ? 1 : -1];
+             char p[-1 - (Bool) 0 < 0 && -1 - (bool) 0 < 0 ? 1 : -1];
              /* Catch a bug in an HP-UX C compiler.  See
                 http://gcc.gnu.org/ml/gcc-patches/2003-12/msg02303.html
                 http://lists.gnu.org/archive/html/bug-coreutils/2005-11/msg00161.html
               */
-             _Bool q = true;
-             _Bool *pq = &q;
+             Bool q = true;
+             Bool *pq = &q;
+             bool *qq = &q;
            ]],
            [[
              bool e = &s;
-             *pq |= q;
-             *pq |= ! q;
+             *pq |= q; *pq |= ! q;
+             *qq |= q; *qq |= ! q;
              /* Refer to every declared value, to avoid compiler optimizations.  */
              return (!a + !b + !c + !d + !e + !f + !g + !h + !i + !!j + !k + !!l
-                     + !m + !n + !o + !p + !q + !pq);
+                     + !m + !n + !o + !p + !q + !pq + !qq);
            ]])],
         [ac_cv_header_stdbool_h=yes],
         [ac_cv_header_stdbool_h=no])])
-- 
2.5.5

Reply via email to