]> Savannah Git Hosting - gnulib.git/commitdiff
nullptr: test for C++ nullptr at configure-time
authorPaul Eggert <eggert@cs.ucla.edu>
Tue, 7 Feb 2023 23:11:32 +0000 (15:11 -0800)
committerPaul Eggert <eggert@cs.ucla.edu>
Tue, 7 Feb 2023 23:14:11 +0000 (15:14 -0800)
* m4/nullptr.m4 (gl_NULLPTR): Test for C++ support for nullptr
at configure-time, as we already do for C support.
This should be more reliable than maintaining #ifdefs by hand.

ChangeLog
m4/nullptr.m4

index defcef16e3fc34115afd66dc2d0fea8c960ac8b0..624afb7e95b35be70c983b5e13a21f47b492e167 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 2023-02-07  Paul Eggert  <eggert@cs.ucla.edu>
 
+       nullptr: test for C++ nullptr at configure-time
+       * m4/nullptr.m4 (gl_NULLPTR): Test for C++ support for nullptr
+       at configure-time, as we already do for C support.
+       This should be more reliable than maintaining #ifdefs by hand.
+
        nullptr: rename from c-nullptr
        * NEWS, doc/gnulib.texi: Mention this.
        * m4/nullptr.m4: Rename from m4/c-nullptr.m4.
index e1218ccd4982313499811f6a2147ea53ee4bbbfd..dda7646906e323c213fa0b72928c6240e4015720 100644 (file)
@@ -1,4 +1,4 @@
-# Check for nullptr that conforms to C23.
+# Check for nullptr that conforms to C23 and C++11.
 
 dnl Copyright 2023 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
@@ -7,35 +7,35 @@ dnl with or without modifications, as long as this notice is preserved.
 
 AC_DEFUN([gl_NULLPTR],
 [
-  AC_CACHE_CHECK([for nullptr], [gl_cv_c_nullptr],
-    [AC_COMPILE_IFELSE(
-       [AC_LANG_SOURCE([[int *p = nullptr;]])],
-       [gl_cv_c_nullptr=yes],
-       [gl_cv_c_nullptr=no])])
+  AS_IF([test ${CC+set}],
+    [AC_CACHE_CHECK([for C nullptr], [gl_cv_c_nullptr],
+       [AC_COMPILE_IFELSE(
+          [AC_LANG_SOURCE([[int *p = nullptr;]])],
+          [gl_cv_c_nullptr=yes],
+          [gl_cv_c_nullptr=no])])])
   if test "$gl_cv_c_nullptr" = yes; then
-    AC_DEFINE([HAVE_C_NULLPTR], [1], [Define to 1 if nullptr works.])
+    AC_DEFINE([HAVE_C_NULLPTR], [1], [Define to 1 if C nullptr works.])
+  fi
+  AS_IF([test ${CXX+set}],
+    [AC_CACHE_CHECK([for C++ nullptr], [gl_cv_cxx_nullptr],
+       [AC_COMPILE_IFELSE(
+          [AC_LANG_SOURCE([[int *p = nullptr;]])],
+          [gl_cv_cxx_nullptr=yes],
+          [gl_cv_cxx_nullptr=no])])])
+  if test "$gl_cv_cxx_nullptr" = yes; then
+    AC_DEFINE([HAVE_CXX_NULLPTR], [1], [Define to 1 if C++ nullptr works.])
   fi
 ])
 
   AH_VERBATIM([nullptr],
 [#ifndef nullptr /* keep config.h idempotent */
-# ifdef __cplusplus
-/* For the C++ compiler the result of the configure test is irrelevant.
-   We know that at least g++ and clang with option -std=c++11 or higher, as well
-   as MSVC 14 or newer, already have nullptr.  */
-#  if !(((defined __GNUC__ || defined __clang__) && __cplusplus >= 201103L) \
-        || (defined _MSC_VER && 1900 <= _MSC_VER))
-/* Define nullptr as a macro, the best we can.  */
-#   if 3 <= __GNUG__
-#    define nullptr __null
-#   else
-#    define nullptr 0L
-#   endif
-#  endif
-# else
-/* For the C compiler, use the result of the configure test.  */
-#  ifndef HAVE_C_NULLPTR
-#   define nullptr ((void *) 0)
+# if !defined __cplusplus && !defined HAVE_C_NULLPTR
+#  define nullptr ((void *) 0)
+# elif defined __cplusplus && !defined HAVE_CXX_NULLPTR
+#  if 3 <= __GNUG__
+#   define nullptr __null
+#  else
+#   define nullptr 0L
 #  endif
 # endif
 #endif])