From: Bruno Haible Date: Mon, 2 Dec 2024 11:07:49 +0000 (+0100) Subject: examples: Update hello-c++20 to work with g++ >= 14 and clang++ >= 19. X-Git-Tag: v0.24~131 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f405adea038ef7cf34a1b8ed6756782d100086e5;p=thirdparty%2Fgettext.git examples: Update hello-c++20 to work with g++ >= 14 and clang++ >= 19. * 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++. --- diff --git a/gettext-tools/examples/hello-c++20/configure.ac b/gettext-tools/examples/hello-c++20/configure.ac index d82088dd3..63519ce4a 100644 --- a/gettext-tools/examples/hello-c++20/configure.ac +++ b/gettext-tools/examples/hello-c++20/configure.ac @@ -12,13 +12,20 @@ AC_PROG_CXX # Try to find compiler option for ISO C++ 20 support. cat <<\EOF > conftest.cc #include +#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]) diff --git a/gettext-tools/examples/hello-c++20/hello.cc b/gettext-tools/examples/hello-c++20/hello.cc index ee0e24349..03828b55e 100644 --- a/gettext-tools/examples/hello-c++20/hello.cc +++ b/gettext-tools/examples/hello-c++20/hello.cc @@ -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 +// . +// The replacement API, presented in +// , +// 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 #include @@ -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; }