From 7a5061a0527b9fcaeedf48e8f5056dd80e77aa98 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Tue, 7 Feb 2023 15:11:32 -0800 Subject: [PATCH 2/3] 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. --- ChangeLog | 5 +++++ m4/nullptr.m4 | 48 ++++++++++++++++++++++++------------------------ 2 files changed, 29 insertions(+), 24 deletions(-) diff --git a/ChangeLog b/ChangeLog index defcef16e3..624afb7e95 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,10 @@ 2023-02-07 Paul Eggert + 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. diff --git a/m4/nullptr.m4 b/m4/nullptr.m4 index e1218ccd49..dda7646906 100644 --- a/m4/nullptr.m4 +++ b/m4/nullptr.m4 @@ -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]) -- 2.39.1