]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
examples: Update hello-c++20 to work with g++ >= 14 and clang++ >= 19.
authorBruno Haible <bruno@clisp.org>
Mon, 2 Dec 2024 11:07:49 +0000 (12:07 +0100)
committerBruno Haible <bruno@clisp.org>
Mon, 2 Dec 2024 11:07:49 +0000 (12:07 +0100)
* gettext-tools/examples/hello-c++20/configure.ac: Support newer C++ versions.
Try option -std=gnu++26, necessary for newer C++ versions.
* gettext-tools/examples/hello-c++20/hello.cc (main): Use different code in
newer C++.

gettext-tools/examples/hello-c++20/configure.ac
gettext-tools/examples/hello-c++20/hello.cc

index d82088dd35e049a1bda6e2f896cd02e9075cbffa..63519ce4a34dc91596e855931e801ee72a925f7e 100644 (file)
@@ -12,13 +12,20 @@ AC_PROG_CXX
 # Try to find compiler option for ISO C++ 20 support.
 cat <<\EOF > conftest.cc
 #include <format>
+#if __cpp_lib_format <= 202106L
 using std::vformat;
 using std::make_format_args;
+#else
+using std::format;
+using std::runtime_format;
+#endif
 EOF
 if ${CXX} ${CXXFLAGS} ${CPPFLAGS} -c conftest.cc 2>/dev/null; then
   :
 elif ${CXX} ${CXXFLAGS} -std=gnu++20 ${CPPFLAGS} -c conftest.cc 2>/dev/null; then
   CXXFLAGS="${CXXFLAGS} -std=gnu++20"
+elif ${CXX} ${CXXFLAGS} -std=gnu++26 ${CPPFLAGS} -c conftest.cc 2>/dev/null; then
+  CXXFLAGS="${CXXFLAGS} -std=gnu++26"
 fi
 
 AM_GNU_GETTEXT([external])
index ee0e243498ad828da0cb34312d4f503c80c1b454..03828b55e3c51154795019806f11db2b53f91878 100644 (file)
@@ -3,6 +3,18 @@
 
 // Source code of the ISO C++ 20 program.
 
+// Note: The API has changed three years after ISO C++ 20. Code that was working
+// fine with g++ 13.1, 13.2 and clang++ 17, 18 (with option -std=gnu++20)
+// no longer compiles with g++ 13.3 or newer and clang++ 19 or newer.  Thus the
+// need to test __cpp_lib_format, whose value is 202106L for the older compilers
+// and 202110L for the newer compilers.  See
+// <https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2905r2.html>.
+// The replacement API, presented in
+// <https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2918r2.html>,
+// uses a new symbol std::runtime_format, that
+//   - does not exist in g++ 13.3,
+//   - exists in g++ 14 or newer and clang++ 19 or newer, but requires the
+//     option -std=gnu++26.
 
 #include <format>
 #include <iostream>
@@ -34,7 +46,12 @@ main ()
   bindtextdomain ("hello-c++20", LOCALEDIR);
 
   cout << _("Hello, world!") << endl;
+#if __cpp_lib_format <= 202106L
   cout << vformat (_("This program is running as process number {:d}."),
                    make_format_args (getpid ()))
+#else
+  cout << format (runtime_format (_("This program is running as process number {:d}.")),
+                  getpid ())
+#endif
        << endl;
 }