doc: Remove references to POSIX 202x.
[gnulib.git] / m4 / std-gnu11.m4
blob37324c158e7cf2e75e7340f71d530146e8effe71
1 # std-gnu11.m4
2 # serial 1
4 # Prefer GNU C11 and C++11 to earlier versions.  -*- coding: utf-8 -*-
6 # This implementation is taken from GNU Autoconf lib/autoconf/c.m4
7 # commit 017d5ddd82854911f0119691d91ea8a1438824d6
8 # dated Sun Apr 3 13:57:17 2016 -0700
9 # This implementation will be obsolete once we can assume Autoconf 2.70
10 # or later is installed everywhere a Gnulib program might be developed.
12 m4_version_prereq([2.70], [], [
15 # Copyright (C) 2001-2024 Free Software Foundation, Inc.
17 # This program is free software; you can redistribute it and/or modify
18 # it under the terms of the GNU General Public License as published by
19 # the Free Software Foundation, either version 3 of the License, or
20 # (at your option) any later version.
22 # This program is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 # GNU General Public License for more details.
27 # You should have received a copy of the GNU General Public License
28 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
30 # Written by David MacKenzie, with help from
31 # Akim Demaille, Paul Eggert,
32 # François Pinard, Karl Berry, Richard Pixley, Ian Lance Taylor,
33 # Roland McGrath, Noah Friedman, david d zuhn, and many others.
36 # AC_PROG_CC([COMPILER ...])
37 # --------------------------
38 # COMPILER ... is a space separated list of C compilers to search for.
39 # This just gives the user an opportunity to specify an alternative
40 # search list for the C compiler.
41 AC_DEFUN_ONCE([AC_PROG_CC],
42 [AC_LANG_PUSH(C)dnl
43 AC_ARG_VAR([CC],     [C compiler command])dnl
44 AC_ARG_VAR([CFLAGS], [C compiler flags])dnl
45 _AC_ARG_VAR_LDFLAGS()dnl
46 _AC_ARG_VAR_LIBS()dnl
47 _AC_ARG_VAR_CPPFLAGS()dnl
48 m4_ifval([$1],
49       [AC_CHECK_TOOLS(CC, [$1])],
50 [AC_CHECK_TOOL(CC, gcc)
51 if test -z "$CC"; then
52   dnl Here we want:
53   dnl   AC_CHECK_TOOL(CC, cc)
54   dnl but without the check for a tool without the prefix.
55   dnl Until the check is removed from there, copy the code:
56   if test -n "$ac_tool_prefix"; then
57     AC_CHECK_PROG(CC, [${ac_tool_prefix}cc], [${ac_tool_prefix}cc])
58   fi
60 if test -z "$CC"; then
61   AC_CHECK_PROG(CC, cc, cc, , , /usr/ucb/cc)
63 if test -z "$CC"; then
64   AC_CHECK_TOOLS(CC, cl.exe)
66 if test -z "$CC"; then
67   AC_CHECK_TOOL(CC, clang)
71 test -z "$CC" && AC_MSG_FAILURE([no acceptable C compiler found in \$PATH])
73 # Provide some information about the compiler.
74 _AS_ECHO_LOG([checking for _AC_LANG compiler version])
75 set X $ac_compile
76 ac_compiler=$[2]
77 for ac_option in --version -v -V -qversion -version; do
78   _AC_DO_LIMIT([$ac_compiler $ac_option >&AS_MESSAGE_LOG_FD])
79 done
81 m4_expand_once([_AC_COMPILER_EXEEXT])[]dnl
82 m4_expand_once([_AC_COMPILER_OBJEXT])[]dnl
83 _AC_LANG_COMPILER_GNU
84 if test $ac_compiler_gnu = yes; then
85   GCC=yes
86 else
87   GCC=
89 _AC_PROG_CC_G
90 dnl
91 dnl Set ac_prog_cc_stdc to the supported C version.
92 dnl Also set the documented variable ac_cv_prog_cc_stdc;
93 dnl its name was chosen when it was cached, but it is no longer cached.
94 _AC_PROG_CC_C11([ac_prog_cc_stdc=c11
95                  ac_cv_prog_cc_stdc=$ac_cv_prog_cc_c11],
96   [_AC_PROG_CC_C99([ac_prog_cc_stdc=c99
97                     ac_cv_prog_cc_stdc=$ac_cv_prog_cc_c99],
98      [_AC_PROG_CC_C89([ac_prog_cc_stdc=c89
99                        ac_cv_prog_cc_stdc=$ac_cv_prog_cc_c89],
100                       [ac_prog_cc_stdc=no
101                        ac_cv_prog_cc_stdc=no])])])
103 AC_LANG_POP(C)dnl
104 ])# AC_PROG_CC
108 # AC_PROG_CXX([LIST-OF-COMPILERS])
109 # --------------------------------
110 # LIST-OF-COMPILERS is a space separated list of C++ compilers to search
111 # for (if not specified, a default list is used).  This just gives the
112 # user an opportunity to specify an alternative search list for the C++
113 # compiler.
114 # aCC   HP-UX C++ compiler much better than `CC', so test before.
115 # FCC   Fujitsu C++ compiler
116 # KCC   KAI C++ compiler
117 # RCC   Rational C++
118 # xlC_r AIX C Set++ (with support for reentrant code)
119 # xlC   AIX C Set++
120 AC_DEFUN([AC_PROG_CXX],
121 [AC_LANG_PUSH(C++)dnl
122 AC_ARG_VAR([CXX],      [C++ compiler command])dnl
123 AC_ARG_VAR([CXXFLAGS], [C++ compiler flags])dnl
124 _AC_ARG_VAR_LDFLAGS()dnl
125 _AC_ARG_VAR_LIBS()dnl
126 _AC_ARG_VAR_CPPFLAGS()dnl
127 _AC_ARG_VAR_PRECIOUS([CCC])dnl
128 if test -z "$CXX"; then
129   if test -n "$CCC"; then
130     CXX=$CCC
131   else
132     AC_CHECK_TOOLS(CXX,
133                    [m4_default([$1],
134                                [g++ c++ gpp aCC CC cxx cc++ cl.exe FCC KCC RCC xlC_r xlC clang++])],
135                    g++)
136   fi
138 # Provide some information about the compiler.
139 _AS_ECHO_LOG([checking for _AC_LANG compiler version])
140 set X $ac_compile
141 ac_compiler=$[2]
142 for ac_option in --version -v -V -qversion; do
143   _AC_DO_LIMIT([$ac_compiler $ac_option >&AS_MESSAGE_LOG_FD])
144 done
146 m4_expand_once([_AC_COMPILER_EXEEXT])[]dnl
147 m4_expand_once([_AC_COMPILER_OBJEXT])[]dnl
148 _AC_LANG_COMPILER_GNU
149 if test $ac_compiler_gnu = yes; then
150   GXX=yes
151 else
152   GXX=
154 _AC_PROG_CXX_G
155 _AC_PROG_CXX_CXX11([ac_prog_cxx_stdcxx=cxx11
156                     ac_cv_prog_cxx_stdcxx=$ac_cv_prog_cxx_cxx11
157                     ac_cv_prog_cxx_cxx98=$ac_cv_prog_cxx_cxx11],
158    [_AC_PROG_CXX_CXX98([ac_prog_cxx_stdcxx=cxx98
159                         ac_cv_prog_cxx_stdcxx=$ac_cv_prog_cxx_cxx98],
160                        [ac_prog_cxx_stdcxx=no
161                         ac_cv_prog_cxx_stdcxx=no])])
162 AC_LANG_POP(C++)dnl
163 ])# AC_PROG_CXX
166 # _AC_C_STD_TRY(STANDARD, TEST-PROLOGUE, TEST-BODY, OPTION-LIST,
167 #               ACTION-IF-AVAILABLE, ACTION-IF-UNAVAILABLE)
168 # --------------------------------------------------------------
169 # Check whether the C compiler accepts features of STANDARD (e.g `c89', `c99')
170 # by trying to compile a program of TEST-PROLOGUE and TEST-BODY.  If this fails,
171 # try again with each compiler option in the space-separated OPTION-LIST; if one
172 # helps, append it to CC.  If eventually successful, run ACTION-IF-AVAILABLE,
173 # else ACTION-IF-UNAVAILABLE.
174 AC_DEFUN([_AC_C_STD_TRY],
175 [AC_MSG_CHECKING([for $CC option to enable ]m4_translit($1, [c], [C])[ features])
176 AC_CACHE_VAL(ac_cv_prog_cc_$1,
177 [ac_cv_prog_cc_$1=no
178 ac_save_CC=$CC
179 AC_LANG_CONFTEST([AC_LANG_PROGRAM([$2], [$3])])
180 for ac_arg in '' $4
182   CC="$ac_save_CC $ac_arg"
183   _AC_COMPILE_IFELSE([], [ac_cv_prog_cc_$1=$ac_arg])
184   test "x$ac_cv_prog_cc_$1" != "xno" && break
185 done
186 rm -f conftest.$ac_ext
187 CC=$ac_save_CC
188 ])# AC_CACHE_VAL
189 ac_prog_cc_stdc_options=
190 case "x$ac_cv_prog_cc_$1" in
191   x)
192     AC_MSG_RESULT([none needed]) ;;
193   xno)
194     AC_MSG_RESULT([unsupported]) ;;
195   *)
196     ac_prog_cc_stdc_options=" $ac_cv_prog_cc_$1"
197     CC=$CC$ac_prog_cc_stdc_options
198     AC_MSG_RESULT([$ac_cv_prog_cc_$1]) ;;
199 esac
200 AS_IF([test "x$ac_cv_prog_cc_$1" != xno], [$5], [$6])
201 ])# _AC_C_STD_TRY
203 # _AC_C_C99_TEST_HEADER
204 # ---------------------
205 # A C header suitable for testing for C99.
206 AC_DEFUN([_AC_C_C99_TEST_HEADER],
207 [[#include <stdarg.h>
208 #include <stdbool.h>
209 #include <stddef.h>
210 #include <stdlib.h>
211 #include <wchar.h>
212 #include <stdio.h>
214 // Check varargs macros.  These examples are taken from C99 6.10.3.5.
215 #define debug(...) fprintf (stderr, __VA_ARGS__)
216 #define showlist(...) puts (#__VA_ARGS__)
217 #define report(test,...) ((test) ? puts (#test) : printf (__VA_ARGS__))
218 static void
219 test_varargs_macros (void)
221   int x = 1234;
222   int y = 5678;
223   debug ("Flag");
224   debug ("X = %d\n", x);
225   showlist (The first, second, and third items.);
226   report (x>y, "x is %d but y is %d", x, y);
229 // Check long long types.
230 #define BIG64 18446744073709551615ull
231 #define BIG32 4294967295ul
232 #define BIG_OK (BIG64 / BIG32 == 4294967297ull && BIG64 % BIG32 == 0)
233 #if !BIG_OK
234   your preprocessor is broken;
235 #endif
236 #if BIG_OK
237 #else
238   your preprocessor is broken;
239 #endif
240 static long long int bignum = -9223372036854775807LL;
241 static unsigned long long int ubignum = BIG64;
243 struct incomplete_array
245   int datasize;
246   double data[];
249 struct named_init {
250   int number;
251   const wchar_t *name;
252   double average;
255 typedef const char *ccp;
257 static inline int
258 test_restrict (ccp restrict text)
260   // See if C++-style comments work.
261   // Iterate through items via the restricted pointer.
262   // Also check for declarations in for loops.
263   for (unsigned int i = 0; *(text+i) != '\0'; ++i)
264     continue;
265   return 0;
268 // Check varargs and va_copy.
269 static bool
270 test_varargs (const char *format, ...)
272   va_list args;
273   va_start (args, format);
274   va_list args_copy;
275   va_copy (args_copy, args);
277   const char *str = "";
278   int number = 0;
279   float fnumber = 0;
281   while (*format)
282     {
283       switch (*format++)
284         {
285         case 's': // string
286           str = va_arg (args_copy, const char *);
287           break;
288         case 'd': // int
289           number = va_arg (args_copy, int);
290           break;
291         case 'f': // float
292           fnumber = va_arg (args_copy, double);
293           break;
294         default:
295           break;
296         }
297     }
298   va_end (args_copy);
299   va_end (args);
301   return *str && number && fnumber;
302 }]])# _AC_C_C99_TEST_HEADER
304 # _AC_C_C99_TEST_BODY
305 # -------------------
306 # A C body suitable for testing for C99, assuming the corresponding header.
307 AC_DEFUN([_AC_C_C99_TEST_BODY],
309   // Check bool.
310   _Bool success = false;
312   // Check restrict.
313   if (test_restrict ("String literal") == 0)
314     success = true;
315   char *restrict newvar = "Another string";
317   // Check varargs.
318   success &= test_varargs ("s, d' f .", "string", 65, 34.234);
319   test_varargs_macros ();
321   // Check flexible array members.
322   struct incomplete_array *ia =
323     malloc (sizeof (struct incomplete_array) + (sizeof (double) * 10));
324   ia->datasize = 10;
325   for (int i = 0; i < ia->datasize; ++i)
326     ia->data[i] = i * 1.234;
328   // Check named initializers.
329   struct named_init ni = {
330     .number = 34,
331     .name = L"Test wide string",
332     .average = 543.34343,
333   };
335   ni.number = 58;
337   int dynamic_array[ni.number];
338   dynamic_array[ni.number - 1] = 543;
340   // work around unused variable warnings
341   return (!success || bignum == 0LL || ubignum == 0uLL || newvar[0] == 'x'
342           || dynamic_array[ni.number - 1] != 543);
345 # _AC_PROG_CC_C99 ([ACTION-IF-AVAILABLE], [ACTION-IF-UNAVAILABLE])
346 # ----------------------------------------------------------------
347 # If the C compiler is not in ISO C99 mode by default, try to add an
348 # option to output variable CC to make it so.  This macro tries
349 # various options that select ISO C99 on some system or another.  It
350 # considers the compiler to be in ISO C99 mode if it handles _Bool,
351 # // comments, flexible array members, inline, long long int, mixed
352 # code and declarations, named initialization of structs, restrict,
353 # va_copy, varargs macros, variable declarations in for loops and
354 # variable length arrays.
355 AC_DEFUN([_AC_PROG_CC_C99],
356 [_AC_C_STD_TRY([c99],
357 [_AC_C_C99_TEST_HEADER],
358 [_AC_C_C99_TEST_BODY],
359 dnl Try
360 dnl GCC         -std=gnu99 (unused restrictive modes: -std=c99 -std=iso9899:1999)
361 dnl IBM XL C    -qlanglvl=extc1x (V12.1; does not pass C11 test)
362 dnl IBM XL C    -qlanglvl=extc99
363 dnl             (pre-V12.1; unused restrictive mode: -qlanglvl=stdc99)
364 dnl HP cc       -AC99
365 dnl Intel ICC   -std=c99, -c99 (deprecated)
366 dnl IRIX        -c99
367 dnl Solaris     -D_STDC_C99=
368 dnl             cc's -xc99 option uses linker magic to define the external
369 dnl             symbol __xpg4 as if by "int __xpg4 = 1;", which enables C99
370 dnl             behavior for C library functions.  This is not wanted here,
371 dnl             because it means that a single module compiled with -xc99
372 dnl             alters C runtime behavior for the entire program, not for
373 dnl             just the module.  Instead, define the (private) symbol
374 dnl             _STDC_C99, which suppresses a bogus failure in <stdbool.h>.
375 dnl             The resulting compiler passes the test case here, and that's
376 dnl             good enough.  For more, please see the thread starting at:
377 dnl            https://lists.gnu.org/r/autoconf/2010-12/msg00059.html
378 dnl Tru64       -c99
379 dnl with extended modes being tried first.
380 [[-std=gnu99 -std=c99 -c99 -AC99 -D_STDC_C99= -qlanglvl=extc1x -qlanglvl=extc99]], [$1], [$2])[]dnl
381 ])# _AC_PROG_CC_C99
384 # _AC_PROG_CC_C11 ([ACTION-IF-AVAILABLE], [ACTION-IF-UNAVAILABLE])
385 # ----------------------------------------------------------------
386 # If the C compiler is not in ISO C11 mode by default, try to add an
387 # option to output variable CC to make it so.  This macro tries
388 # various options that select ISO C11 on some system or another.  It
389 # considers the compiler to be in ISO C11 mode if it handles _Alignas,
390 # _Alignof, _Noreturn, _Static_assert, UTF-8 string literals,
391 # duplicate typedefs, and anonymous structures and unions.
392 AC_DEFUN([_AC_PROG_CC_C11],
393 [_AC_C_STD_TRY([c11],
394 [_AC_C_C99_TEST_HEADER[
395 // Check _Alignas.
396 char _Alignas (double) aligned_as_double;
397 char _Alignas (0) no_special_alignment;
398 extern char aligned_as_int;
399 char _Alignas (0) _Alignas (int) aligned_as_int;
401 // Check _Alignof.
402 enum
404   int_alignment = _Alignof (int),
405   int_array_alignment = _Alignof (int[100]),
406   char_alignment = _Alignof (char)
408 _Static_assert (0 < -_Alignof (int), "_Alignof is signed");
410 // Check _Noreturn.
411 int _Noreturn does_not_return (void) { for (;;) continue; }
413 // Check _Static_assert.
414 struct test_static_assert
416   int x;
417   _Static_assert (sizeof (int) <= sizeof (long int),
418                   "_Static_assert does not work in struct");
419   long int y;
422 // Check UTF-8 literals.
423 #define u8 syntax error!
424 char const utf8_literal[] = u8"happens to be ASCII" "another string";
426 // Check duplicate typedefs.
427 typedef long *long_ptr;
428 typedef long int *long_ptr;
429 typedef long_ptr long_ptr;
431 // Anonymous structures and unions -- taken from C11 6.7.2.1 Example 1.
432 struct anonymous
434   union {
435     struct { int i; int j; };
436     struct { int k; long int l; } w;
437   };
438   int m;
439 } v1;
441 [_AC_C_C99_TEST_BODY[
442   v1.i = 2;
443   v1.w.k = 5;
444   _Static_assert ((offsetof (struct anonymous, i)
445                    == offsetof (struct anonymous, w.k)),
446                   "Anonymous union alignment botch");
448 dnl Try
449 dnl GCC         -std=gnu11 (unused restrictive mode: -std=c11)
450 dnl with extended modes being tried first.
452 dnl Do not try -qlanglvl=extc1x, because IBM XL C V12.1 (the latest version as
453 dnl of September 2012) does not pass the C11 test.  For now, try extc1x when
454 dnl compiling the C99 test instead, since it enables _Static_assert and
455 dnl _Noreturn, which is a win.  If -qlanglvl=extc11 or -qlanglvl=extc1x passes
456 dnl the C11 test in some future version of IBM XL C, we'll add it here,
457 dnl preferably extc11.
458 [[-std=gnu11]], [$1], [$2])[]dnl
459 ])# _AC_PROG_CC_C11
462 # AC_PROG_CC_C89
463 # --------------
464 # Do not use AU_ALIAS here and in AC_PROG_CC_C99 and AC_PROG_CC_STDC,
465 # as that'd be incompatible with how Automake redefines AC_PROG_CC.  See
466 # <https://lists.gnu.org/r/autoconf/2012-10/msg00048.html>.
467 AU_DEFUN([AC_PROG_CC_C89],
468   [AC_REQUIRE([AC_PROG_CC])],
469   [$0 is obsolete; use AC_PROG_CC]
472 # AC_PROG_CC_C99
473 # --------------
474 AU_DEFUN([AC_PROG_CC_C99],
475   [AC_REQUIRE([AC_PROG_CC])],
476   [$0 is obsolete; use AC_PROG_CC]
479 # AC_PROG_CC_STDC
480 # ---------------
481 AU_DEFUN([AC_PROG_CC_STDC],
482   [AC_REQUIRE([AC_PROG_CC])],
483   [$0 is obsolete; use AC_PROG_CC]
487 # AC_C_PROTOTYPES
488 # ---------------
489 # Check if the C compiler supports prototypes, included if it needs
490 # options.
491 AC_DEFUN([AC_C_PROTOTYPES],
492 [AC_REQUIRE([AC_PROG_CC])dnl
493 if test "$ac_prog_cc_stdc" != no; then
494   AC_DEFINE(PROTOTYPES, 1,
495             [Define to 1 if the C compiler supports function prototypes.])
496   AC_DEFINE(__PROTOTYPES, 1,
497             [Define like PROTOTYPES; this can be used by system headers.])
499 ])# AC_C_PROTOTYPES
502 # _AC_CXX_STD_TRY(STANDARD, TEST-PROLOGUE, TEST-BODY, OPTION-LIST,
503 #                 ACTION-IF-AVAILABLE, ACTION-IF-UNAVAILABLE)
504 # ----------------------------------------------------------------
505 # Check whether the C++ compiler accepts features of STANDARD (e.g
506 # `cxx98', `cxx11') by trying to compile a program of TEST-PROLOGUE
507 # and TEST-BODY.  If this fails, try again with each compiler option
508 # in the space-separated OPTION-LIST; if one helps, append it to CXX.
509 # If eventually successful, run ACTION-IF-AVAILABLE, else
510 # ACTION-IF-UNAVAILABLE.
511 AC_DEFUN([_AC_CXX_STD_TRY],
512 [AC_MSG_CHECKING([for $CXX option to enable ]m4_translit(m4_translit($1, [x], [+]), [a-z], [A-Z])[ features])
513 AC_LANG_PUSH(C++)dnl
514 AC_CACHE_VAL(ac_cv_prog_cxx_$1,
515 [ac_cv_prog_cxx_$1=no
516 ac_save_CXX=$CXX
517 AC_LANG_CONFTEST([AC_LANG_PROGRAM([$2], [$3])])
518 for ac_arg in '' $4
520   CXX="$ac_save_CXX $ac_arg"
521   _AC_COMPILE_IFELSE([], [ac_cv_prog_cxx_$1=$ac_arg])
522   test "x$ac_cv_prog_cxx_$1" != "xno" && break
523 done
524 rm -f conftest.$ac_ext
525 CXX=$ac_save_CXX
526 ])# AC_CACHE_VAL
527 ac_prog_cxx_stdcxx_options=
528 case "x$ac_cv_prog_cxx_$1" in
529   x)
530     AC_MSG_RESULT([none needed]) ;;
531   xno)
532     AC_MSG_RESULT([unsupported]) ;;
533   *)
534     ac_prog_cxx_stdcxx_options=" $ac_cv_prog_cxx_$1"
535     CXX=$CXX$ac_prog_cxx_stdcxx_options
536     AC_MSG_RESULT([$ac_cv_prog_cxx_$1]) ;;
537 esac
538 AC_LANG_POP(C++)dnl
539 AS_IF([test "x$ac_cv_prog_cxx_$1" != xno], [$5], [$6])
540 ])# _AC_CXX_STD_TRY
542 # _AC_CXX_CXX98_TEST_HEADER
543 # -------------------------
544 # A C++ header suitable for testing for CXX98.
545 AC_DEFUN([_AC_CXX_CXX98_TEST_HEADER],
547 #include <algorithm>
548 #include <cstdlib>
549 #include <fstream>
550 #include <iomanip>
551 #include <iostream>
552 #include <list>
553 #include <map>
554 #include <set>
555 #include <sstream>
556 #include <stdexcept>
557 #include <string>
558 #include <utility>
559 #include <vector>
561 namespace test {
562   typedef std::vector<std::string> string_vec;
563   typedef std::pair<int,bool> map_value;
564   typedef std::map<std::string,map_value> map_type;
565   typedef std::set<int> set_type;
567   template<typename T>
568   class printer {
569   public:
570     printer(std::ostringstream& os): os(os) {}
571     void operator() (T elem) { os << elem << std::endl; }
572   private:
573     std::ostringstream& os;
574   };
576 ]])# _AC_CXX_CXX98_TEST_HEADER
578 # _AC_CXX_CXX98_TEST_BODY
579 # -----------------------
580 # A C++ body suitable for testing for CXX98, assuming the corresponding header.
581 AC_DEFUN([_AC_CXX_CXX98_TEST_BODY],
584 try {
585   // Basic string.
586   std::string teststr("ASCII text");
587   teststr += " string";
589   // Simple vector.
590   test::string_vec testvec;
591   testvec.push_back(teststr);
592   testvec.push_back("foo");
593   testvec.push_back("bar");
594   if (testvec.size() != 3) {
595     throw std::runtime_error("vector size is not 1");
596   }
598   // Dump vector into stringstream and obtain string.
599   std::ostringstream os;
600   for (test::string_vec::const_iterator i = testvec.begin();
601        i != testvec.end(); ++i) {
602     if (i + 1 != testvec.end()) {
603       os << teststr << '\n';
604     }
605   }
606   // Check algorithms work.
607   std::for_each(testvec.begin(), testvec.end(), test::printer<std::string>(os));
608   std::string os_out = os.str();
610   // Test pair and map.
611   test::map_type testmap;
612   testmap.insert(std::make_pair(std::string("key"),
613                                 std::make_pair(53,false)));
615   // Test set.
616   int values[] = {9, 7, 13, 15, 4, 18, 12, 10, 5, 3, 14, 19, 17, 8, 6, 20, 16, 2, 11, 1};
617   test::set_type testset(values, values + sizeof(values)/sizeof(values[0]));
618   std::list<int> testlist(testset.begin(), testset.end());
619   std::copy(testset.begin(), testset.end(), std::back_inserter(testlist));
620 } catch (const std::exception& e) {
621   std::cerr << "Caught exception: " << e.what() << std::endl;
623   // Test fstream
624   std::ofstream of("test.txt");
625   of << "Test ASCII text\n" << std::flush;
626   of << "N= " << std::hex << std::setw(8) << std::left << 534 << std::endl;
627   of.close();
629 std::exit(0);
632 # _AC_CXX_CXX11_TEST_HEADER
633 # -------------------------
634 # A C++ header suitable for testing for CXX11.
635 AC_DEFUN([_AC_CXX_CXX11_TEST_HEADER],
637 #include <deque>
638 #include <functional>
639 #include <memory>
640 #include <tuple>
641 #include <array>
642 #include <regex>
643 #include <iostream>
645 namespace cxx11test
647   typedef std::shared_ptr<std::string> sptr;
648   typedef std::weak_ptr<std::string> wptr;
650   typedef std::tuple<std::string,int,double> tp;
651   typedef std::array<int, 20> int_array;
653   constexpr int get_val() { return 20; }
655   struct testinit
656   {
657     int i;
658     double d;
659   };
661   class delegate  {
662   public:
663     delegate(int n) : n(n) {}
664     delegate(): delegate(2354) {}
666     virtual int getval() { return this->n; };
667   protected:
668     int n;
669   };
671   class overridden : public delegate {
672   public:
673     overridden(int n): delegate(n) {}
674     virtual int getval() override final { return this->n * 2; }
675   };
677   class nocopy {
678   public:
679     nocopy(int i): i(i) {}
680     nocopy() = default;
681     nocopy(const nocopy&) = delete;
682     nocopy & operator=(const nocopy&) = delete;
683   private:
684     int i;
685   };
687 ]])# _AC_CXX_CXX11_TEST_HEADER
689 # _AC_CXX_CXX11_TEST_BODY
690 # -----------------------
691 # A C++ body suitable for testing for CXX11, assuming the corresponding header.
692 AC_DEFUN([_AC_CXX_CXX11_TEST_BODY],
695   // Test auto and decltype
696   std::deque<int> d;
697   d.push_front(43);
698   d.push_front(484);
699   d.push_front(3);
700   d.push_front(844);
701   int total = 0;
702   for (auto i = d.begin(); i != d.end(); ++i) { total += *i; }
704   auto a1 = 6538;
705   auto a2 = 48573953.4;
706   auto a3 = "String literal";
708   decltype(a2) a4 = 34895.034;
711   // Test constexpr
712   short sa[cxx11test::get_val()] = { 0 };
715   // Test initializer lists
716   cxx11test::testinit il = { 4323, 435234.23544 };
719   // Test range-based for and lambda
720   cxx11test::int_array array = {9, 7, 13, 15, 4, 18, 12, 10, 5, 3, 14, 19, 17, 8, 6, 20, 16, 2, 11, 1};
721   for (int &x : array) { x += 23; }
722   std::for_each(array.begin(), array.end(), [](int v1){ std::cout << v1; });
725   using cxx11test::sptr;
726   using cxx11test::wptr;
728   sptr sp(new std::string("ASCII string"));
729   wptr wp(sp);
730   sptr sp2(wp);
733   cxx11test::tp tuple("test", 54, 45.53434);
734   double d = std::get<2>(tuple);
735   std::string s;
736   int i;
737   std::tie(s,i,d) = tuple;
740   static std::regex filename_regex("^_?([a-z0-9_.]+-)+[a-z0-9]+$");
741   std::string testmatch("Test if this string matches");
742   bool match = std::regex_search(testmatch, filename_regex);
745   cxx11test::int_array array = {9, 7, 13, 15, 4, 18, 12, 10, 5, 3, 14, 19, 17, 8, 6, 20, 16, 2, 11, 1};
746   cxx11test::int_array::size_type size = array.size();
749   // Test constructor delegation
750   cxx11test::delegate d1;
751   cxx11test::delegate d2();
752   cxx11test::delegate d3(45);
755   // Test override and final
756   cxx11test::overridden o1(55464);
759   // Test nullptr
760   char *c = nullptr;
763   // Test template brackets
764   std::vector<std::pair<int,char*>> v1;
767   // Unicode literals
768   char const *utf8 = u8"UTF-8 string \u2500";
769   char16_t const *utf16 = u"UTF-8 string \u2500";
770   char32_t const *utf32 = U"UTF-32 string \u2500";
774 # _AC_PROG_CXX_CXX98 ([ACTION-IF-AVAILABLE], [ACTION-IF-UNAVAILABLE])
775 # -------------------------------------------------------------------
777 # If the C++ compiler is not in ISO C++98 mode by default, try to add
778 # an option to output variable CXX to make it so.  This macro tries
779 # various options that select ISO C++98 on some system or another.  It
780 # considers the compiler to be in ISO C++98 mode if it handles basic
781 # features of the std namespace including: string, containers (list,
782 # map, set, vector), streams (fstreams, iostreams, stringstreams,
783 # iomanip), pair, exceptions and algorithms.
786 AC_DEFUN([_AC_PROG_CXX_CXX98],
787 [_AC_CXX_STD_TRY([cxx98],
788 [_AC_CXX_CXX98_TEST_HEADER],
789 [_AC_CXX_CXX98_TEST_BODY],
790 dnl Try
791 dnl GCC         -std=gnu++98 (unused restrictive mode: -std=c++98)
792 dnl IBM XL C    -qlanglvl=extended
793 dnl HP aC++     -AA
794 dnl Intel ICC   -std=gnu++98
795 dnl Solaris     N/A (default)
796 dnl Tru64       N/A (default, but -std gnu could be used)
797 dnl with extended modes being tried first.
798 [[-std=gnu++98 -std=c++98 -qlanglvl=extended -AA]], [$1], [$2])[]dnl
799 ])# _AC_PROG_CXX_CXX98
801 # _AC_PROG_CXX_CXX11 ([ACTION-IF-AVAILABLE], [ACTION-IF-UNAVAILABLE])
802 # -------------------------------------------------------------------
803 # If the C++ compiler is not in ISO CXX11 mode by default, try to add
804 # an option to output variable CXX to make it so.  This macro tries
805 # various options that select ISO C++11 on some system or another.  It
806 # considers the compiler to be in ISO C++11 mode if it handles all the
807 # tests from the C++98 checks, plus the following: Language features
808 # (auto, constexpr, decltype, default/deleted constructors, delegate
809 # constructors, final, initializer lists, lambda functions, nullptr,
810 # override, range-based for loops, template brackets without spaces,
811 # unicode literals) and library features (array, memory (shared_ptr,
812 # weak_ptr), regex and tuple types).
813 AC_DEFUN([_AC_PROG_CXX_CXX11],
814 [_AC_CXX_STD_TRY([cxx11],
815 [_AC_CXX_CXX11_TEST_HEADER
816 _AC_CXX_CXX98_TEST_HEADER],
817 [_AC_CXX_CXX11_TEST_BODY
818 _AC_CXX_CXX98_TEST_BODY],
819 dnl Try
820 dnl GCC         -std=gnu++11 (unused restrictive mode: -std=c++11) [and 0x variants]
821 dnl IBM XL C    -qlanglvl=extended0x
822 dnl             (pre-V12.1; unused restrictive mode: -qlanglvl=stdcxx11)
823 dnl HP aC++     -AA
824 dnl Intel ICC   -std=c++11 -std=c++0x
825 dnl Solaris     N/A (no support)
826 dnl Tru64       N/A (no support)
827 dnl with extended modes being tried first.
828 [[-std=gnu++11 -std=c++11 -std=gnu++0x -std=c++0x -qlanglvl=extended0x -AA]], [$1], [$2])[]dnl
829 ])# _AC_PROG_CXX_CXX11
832 ])# m4_version_prereq