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.
-# 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
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])