From 2af93c10fe716090f8d8ab1f884f11ee9b9a86cc Mon Sep 17 00:00:00 2001
From: Jelte Fennema-Nio <postgres@jeltef.nl>
Date: Wed, 21 Jan 2026 09:18:53 +0100
Subject: [PATCH v1] Detect if flags are needed for C++11 support

Just like we only support compiling with C11, we only support compiling
extensions with C++11 and up. Some compilers support C++11 but don't
enable it by default. This detects if flags are needed to enable C++11
support, in a similar way to how we check the same for C11 support.
---
 configure    | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 configure.ac | 28 +++++++++++++++++++++++++-
 meson.build  | 28 ++++++++++++++++++++++++++
 3 files changed, 110 insertions(+), 2 deletions(-)

diff --git a/configure b/configure
index 04eeb1a741c..4c6f604c276 100755
--- a/configure
+++ b/configure
@@ -4770,7 +4770,61 @@ ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $
 ac_compiler_gnu=$ac_cv_c_compiler_gnu
 
 
-if test -n "$ac_ct_CXX"; then
+# Detect option needed for C++11
+# PostgreSQL headers use C11 features (like alignas) which require C++11.
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $CXX option to accept ISO C++11" >&5
+$as_echo_n "checking for $CXX option to accept ISO C++11... " >&6; }
+
+if ${pgac_cv_prog_cxx_cxx11+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+  pgac_cv_prog_cxx_cxx11=no
+pgac_save_CXX=$CXX
+ac_ext=cpp
+ac_cpp='$CXXCPP $CPPFLAGS'
+ac_compile='$CXX -c $CXXFLAGS $CPPFLAGS conftest.$ac_ext >&5'
+ac_link='$CXX -o conftest$ac_exeext $CXXFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'
+ac_compiler_gnu=$ac_cv_cxx_compiler_gnu
+
+for pgac_arg in '' '-std=gnu++11' '-std=c++11'; do
+  CXX="$pgac_save_CXX $pgac_arg"
+  cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+#if !defined __cplusplus || __cplusplus < 201103L
+# error "Compiler does not advertise C++11 conformance"
+#endif
+_ACEOF
+if ac_fn_cxx_try_compile "$LINENO"; then :
+  pgac_cv_prog_cxx_cxx11=$pgac_arg
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+  test x"$pgac_cv_prog_cxx_cxx11" != x"no" && break
+done
+ac_ext=c
+ac_cpp='$CPP $CPPFLAGS'
+ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'
+ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'
+ac_compiler_gnu=$ac_cv_c_compiler_gnu
+
+CXX=$pgac_save_CXX
+fi
+
+
+if test x"$pgac_cv_prog_cxx_cxx11" = x"no"; then
+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: unsupported" >&5
+$as_echo "unsupported" >&6; }
+  { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: C++ compiler \"$CXX\" does not support C++11" >&5
+$as_echo "$as_me: WARNING: C++ compiler \"$CXX\" does not support C++11" >&2;}
+elif test x"$pgac_cv_prog_cxx_cxx11" = x""; then
+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: none needed" >&5
+$as_echo "none needed" >&6; }
+else
+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: $pgac_cv_prog_cxx_cxx11" >&5
+$as_echo "$pgac_cv_prog_cxx_cxx11" >&6; }
+  CXX="$CXX $pgac_cv_prog_cxx_cxx11"
+fi
+
+if test -n "$ac_ct_CXX" -a x"$pgac_cv_prog_cxx_cxx11" != x"no"; then
   have_cxx=yes
 else
   have_cxx=no
diff --git a/configure.ac b/configure.ac
index 13c75170f7a..a329484e426 100644
--- a/configure.ac
+++ b/configure.ac
@@ -387,7 +387,33 @@ fi
 
 AC_PROG_CXX([$pgac_cxx_list])
 
-if test -n "$ac_ct_CXX"; then
+# Detect option needed for C++11
+AC_MSG_CHECKING([for $CXX option to accept ISO C++11])
+AC_CACHE_VAL([pgac_cv_prog_cxx_cxx11],
+[pgac_cv_prog_cxx_cxx11=no
+pgac_save_CXX=$CXX
+AC_LANG_PUSH([C++])
+for pgac_arg in '' '-std=gnu++11' '-std=c++11'; do
+  CXX="$pgac_save_CXX $pgac_arg"
+  AC_COMPILE_IFELSE([AC_LANG_SOURCE([[#if !defined __cplusplus || __cplusplus < 201103L
+# error "Compiler does not advertise C++11 conformance"
+#endif]])], [[pgac_cv_prog_cxx_cxx11=$pgac_arg]])
+  test x"$pgac_cv_prog_cxx_cxx11" != x"no" && break
+done
+AC_LANG_POP([C++])
+CXX=$pgac_save_CXX])
+
+if test x"$pgac_cv_prog_cxx_cxx11" = x"no"; then
+  AC_MSG_RESULT([unsupported])
+  AC_MSG_WARN([C++ compiler "$CXX" does not support C++11])
+elif test x"$pgac_cv_prog_cxx_cxx11" = x""; then
+  AC_MSG_RESULT([none needed])
+else
+  AC_MSG_RESULT([$pgac_cv_prog_cxx_cxx11])
+  CXX="$CXX $pgac_cv_prog_cxx_cxx11"
+fi
+
+if test -n "$ac_ct_CXX" -a x"$pgac_cv_prog_cxx_cxx11" != x"no"; then
   have_cxx=yes
 else
   have_cxx=no
diff --git a/meson.build b/meson.build
index 6d304f32fb0..f88a23b9f3a 100644
--- a/meson.build
+++ b/meson.build
@@ -585,6 +585,34 @@ if not cc.compiles(c11_test, name: 'C11')
   endif
 endif
 
+# Do we need an option to enable C++11?
+if have_cxx
+  cxx11_test = '''
+#if !defined __cplusplus || __cplusplus < 201103L
+# error "Compiler does not advertise C++11 conformance"
+#endif
+'''
+
+  if not cxx.compiles(cxx11_test, name: 'C++11')
+    have_cxx = false
+    if cxx.get_id() == 'msvc'
+      cxx11_test_args = ['/std:c++11']
+    else
+      cxx11_test_args = ['-std=gnu++11', '-std=c++11']
+    endif
+    foreach arg : cxx11_test_args
+      if cxx.compiles(cxx11_test, name: 'C++11 with @0@'.format(arg), args: [arg])
+        have_cxx = true
+        cxxflags += arg
+        break
+      endif
+    endforeach
+    if not have_cxx
+      warning('C++ compiler does not support C++11')
+    endif
+  endif
+endif
+
 
 postgres_inc = [include_directories(postgres_inc_d)]
 test_lib_d = postgres_lib_d

base-commit: b4555cb070f134c04328df54ce31d4ef1970f3bb
-- 
2.52.0

