1 # Copyright (c) 2016-2022 Free Software Foundation, Inc.
3 # Originally based on the AX_CXX_COMPILE_STDCXX macro found at the url
6 # Local GDB customizations:
8 # - AC_SUBST CXX_DIALECT instead of changing CXX/CXXCPP.
10 # ===========================================================================
11 # https://www.gnu.org/software/autoconf-archive/ax_cxx_compile_stdcxx.html
12 # ===========================================================================
16 # AX_CXX_COMPILE_STDCXX(VERSION, [ext|noext], [mandatory|optional])
20 # Check for baseline language coverage in the compiler for the specified
21 # version of the C++ standard. If necessary, add switches to CXX and
22 # CXXCPP to enable support. VERSION may be '11' (for the C++11 standard)
23 # or '14' (for the C++14 standard).
25 # The second argument, if specified, indicates whether you insist on an
26 # extended mode (e.g. -std=gnu++11) or a strict conformance mode (e.g.
27 # -std=c++11). If neither is specified, you get whatever works, with
28 # preference for an extended mode.
30 # The third argument, if specified 'mandatory' or if left unspecified,
31 # indicates that baseline support for the specified C++ standard is
32 # required and that the macro should error out if no mode with that
33 # support is found. If specified 'optional', then configuration proceeds
34 # regardless, after defining HAVE_CXX${VERSION} if and only if a
35 # supporting mode is found.
39 # Copyright (c) 2008 Benjamin Kosnik <bkoz@redhat.com>
40 # Copyright (c) 2012 Zack Weinberg <zackw@panix.com>
41 # Copyright (c) 2013 Roy Stogner <roystgnr@ices.utexas.edu>
42 # Copyright (c) 2014, 2015 Google Inc.; contributed by Alexey Sokolov <sokolov@google.com>
43 # Copyright (c) 2015 Paul Norman <penorman@mac.com>
44 # Copyright (c) 2015 Moritz Klammler <moritz@klammler.eu>
45 # Copyright (c) 2016 Krzesimir Nowak <qdlacz@gmail.com>
47 # Copying and distribution of this file, with or without modification, are
48 # permitted in any medium without royalty provided the copyright notice
49 # and this notice are preserved. This file is offered as-is, without any
54 dnl This macro is based on the code from the AX_CXX_COMPILE_STDCXX_11 macro
55 dnl (serial version number 13).
57 AC_DEFUN([AX_CXX_COMPILE_STDCXX], [dnl
58 m4_if([$1], [11], [ax_cxx_compile_alternatives="11 0x"],
59 [$1], [14], [ax_cxx_compile_alternatives="14 1y"],
60 [$1], [17], [ax_cxx_compile_alternatives="17 1z"],
61 [m4_fatal([invalid first argument `$1' to AX_CXX_COMPILE_STDCXX])])dnl
65 [m4_fatal([invalid second argument `$2' to AX_CXX_COMPILE_STDCXX])])dnl
66 m4_if([$3], [], [ax_cxx_compile_cxx$1_required=true],
67 [$3], [mandatory], [ax_cxx_compile_cxx$1_required=true],
68 [$3], [optional], [ax_cxx_compile_cxx$1_required=false],
69 [m4_fatal([invalid third argument `$3' to AX_CXX_COMPILE_STDCXX])])
70 AC_LANG_PUSH([C++])dnl
73 AC_CACHE_CHECK(whether $CXX supports C++$1 features by default,
74 ax_cv_cxx_compile_cxx$1,
75 [AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],
76 [ax_cv_cxx_compile_cxx$1=yes],
77 [ax_cv_cxx_compile_cxx$1=no])])
78 if test x$ax_cv_cxx_compile_cxx$1 = xyes; then
82 m4_if([$2], [noext], [], [dnl
83 if test x$ac_success = xno; then
84 for alternative in ${ax_cxx_compile_alternatives}; do
85 switch="-std=gnu++${alternative}"
86 cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_$switch])
87 AC_CACHE_CHECK(whether $CXX supports C++$1 features with $switch,
91 AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],
95 if eval test x\$$cachevar = xyes; then
98 if test -n "$CXXCPP" ; then
99 CXXCPP="$CXXCPP $switch"
107 m4_if([$2], [ext], [], [dnl
108 if test x$ac_success = xno; then
109 dnl HP's aCC needs +std=c++11 according to:
110 dnl http://h21007.www2.hp.com/portal/download/files/unprot/aCxx/PDF_Release_Notes/769149-001.pdf
111 dnl Cray's crayCC needs "-h std=c++11"
112 for alternative in ${ax_cxx_compile_alternatives}; do
113 for switch in -std=c++${alternative} +std=c++${alternative} "-h std=c++${alternative}"; do
114 cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_$switch])
115 AC_CACHE_CHECK(whether $CXX supports C++$1 features with $switch,
119 AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],
120 [eval $cachevar=yes],
123 if eval test x\$$cachevar = xyes; then
124 CXX_DIALECT="$switch"
126 if test -n "$CXXCPP" ; then
127 CXXCPP="$CXXCPP $switch"
133 if test x$ac_success = xyes; then
139 if test x$ax_cxx_compile_cxx$1_required = xtrue; then
140 if test x$ac_success = xno; then
141 AC_MSG_ERROR([*** A compiler with support for C++$1 language features is required.])
144 if test x$ac_success = xno; then
146 AC_MSG_NOTICE([No compiler with C++$1 support was found])
149 AC_DEFINE(HAVE_CXX$1,1,
150 [define if the compiler supports basic C++$1 syntax])
153 AC_SUBST(CXX_DIALECT)
157 dnl Test body for checking C++11 support
159 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_11],
160 _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
164 dnl Test body for checking C++14 support
166 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_14],
167 _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
168 _AX_CXX_COMPILE_STDCXX_testbody_new_in_14
171 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_17],
172 _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
173 _AX_CXX_COMPILE_STDCXX_testbody_new_in_14
174 _AX_CXX_COMPILE_STDCXX_testbody_new_in_17
177 dnl Tests for new features in C++11
179 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_11], [[
181 // If the compiler admits that it is not ready for C++11, why torture it?
182 // Hopefully, this will speed up the test.
186 #error "This is not a C++ compiler"
188 #elif __cplusplus < 201103L
190 #error "This is not a C++11 compiler"
197 namespace test_static_assert
200 template <typename T>
203 static_assert(sizeof(int) <= sizeof(T), "not big enough");
208 namespace test_final_override
216 struct Derived : public Base
218 virtual void f() override {}
223 namespace test_double_right_angle_brackets
226 template < typename T >
229 typedef check<void> single_type;
230 typedef check<check<void>> double_type;
231 typedef check<check<check<void>>> triple_type;
232 typedef check<check<check<check<void>>>> quadruple_type;
236 namespace test_decltype
249 namespace test_type_deduction
252 template < typename T1, typename T2 >
255 static const bool value = false;
258 template < typename T >
261 static const bool value = true;
264 template < typename T1, typename T2 >
266 add(T1 a1, T2 a2) -> decltype(a1 + a2)
272 test(const int c, volatile int v)
274 static_assert(is_same<int, decltype(0)>::value == true, "");
275 static_assert(is_same<int, decltype(c)>::value == false, "");
276 static_assert(is_same<int, decltype(v)>::value == false, "");
279 auto sumi = ac + av + 'x';
280 auto sumf = ac + av + 1.0;
281 static_assert(is_same<int, decltype(ac)>::value == true, "");
282 static_assert(is_same<int, decltype(av)>::value == true, "");
283 static_assert(is_same<int, decltype(sumi)>::value == true, "");
284 static_assert(is_same<int, decltype(sumf)>::value == false, "");
285 static_assert(is_same<int, decltype(add(c, v))>::value == true, "");
286 return (sumf > 0.0) ? sumi : add(c, v);
291 namespace test_noexcept
294 int f() { return 0; }
295 int g() noexcept { return 0; }
297 static_assert(noexcept(f()) == false, "");
298 static_assert(noexcept(g()) == true, "");
302 namespace test_constexpr
305 template < typename CharT >
306 unsigned long constexpr
307 strlen_c_r(const CharT *const s, const unsigned long acc) noexcept
309 return *s ? strlen_c_r(s + 1, acc + 1) : acc;
312 template < typename CharT >
313 unsigned long constexpr
314 strlen_c(const CharT *const s) noexcept
316 return strlen_c_r(s, 0UL);
319 static_assert(strlen_c("") == 0UL, "");
320 static_assert(strlen_c("1") == 1UL, "");
321 static_assert(strlen_c("example") == 7UL, "");
322 static_assert(strlen_c("another\0example") == 7UL, "");
326 namespace test_rvalue_references
332 static constexpr int value = N;
335 answer<1> f(int&) { return answer<1>(); }
336 answer<2> f(const int&) { return answer<2>(); }
337 answer<3> f(int&&) { return answer<3>(); }
344 static_assert(decltype(f(i))::value == 1, "");
345 static_assert(decltype(f(c))::value == 2, "");
346 static_assert(decltype(f(0))::value == 3, "");
351 namespace test_uniform_initialization
356 static const int zero {};
357 static const int one {1};
360 static_assert(test::zero == 0, "");
361 static_assert(test::one == 1, "");
365 namespace test_lambdas
371 auto lambda1 = [](){};
372 auto lambda2 = lambda1;
380 auto a = [](int i, int j){ return i + j; }(1, 2);
381 auto b = []() -> int { return '0'; }();
382 auto c = [=](){ return a + b; }();
383 auto d = [&](){ return c; }();
384 auto e = [a, &b](int x) mutable {
385 const auto identity = [](int y){ return y; };
386 for (auto i = 0; i < a; ++i)
388 return x + identity(a + b);
390 return a + b + c + d + e;
396 const auto nullary = [](){ return 0; };
397 const auto unary = [](int x){ return x; };
398 using nullary_t = decltype(nullary);
399 using unary_t = decltype(unary);
400 const auto higher1st = [](nullary_t f){ return f(); };
401 const auto higher2nd = [unary](nullary_t f1){
402 return [unary, f1](unary_t f2){ return f2(unary(f1())); };
404 return higher1st(nullary) + higher2nd(nullary)(unary);
409 namespace test_variadic_templates
415 template <int N0, int... N1toN>
416 struct sum<N0, N1toN...>
418 static constexpr auto value = N0 + sum<N1toN...>::value;
424 static constexpr auto value = 0;
427 static_assert(sum<>::value == 0, "");
428 static_assert(sum<1>::value == 1, "");
429 static_assert(sum<23>::value == 23, "");
430 static_assert(sum<1, 2>::value == 3, "");
431 static_assert(sum<5, 5, 11>::value == 21, "");
432 static_assert(sum<2, 3, 5, 7, 11, 13>::value == 41, "");
436 // http://stackoverflow.com/questions/13728184/template-aliases-and-sfinae
437 // Clang 3.1 fails with headers of libstd++ 4.8.3 when using std::function
439 namespace test_template_alias_sfinae
445 using member = typename T::member_type;
451 void func(member<T>*) {}
455 void test() { func<foo>(0); }
461 #endif // __cplusplus >= 201103L
466 dnl Tests for new features in C++14
468 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_14], [[
470 // If the compiler admits that it is not ready for C++14, why torture it?
471 // Hopefully, this will speed up the test.
475 #error "This is not a C++ compiler"
477 #elif __cplusplus < 201402L
479 #error "This is not a C++14 compiler"
486 namespace test_polymorphic_lambdas
492 const auto lambda = [](auto&&... args){
493 const auto istiny = [](auto x){
494 return (sizeof(x) == 1UL) ? 1 : 0;
496 const int aretiny[] = { istiny(args)... };
499 return lambda(1, 1L, 1.0f, '1');
504 namespace test_binary_literals
507 constexpr auto ivii = 0b0000000000101010;
508 static_assert(ivii == 42, "wrong value");
512 namespace test_generalized_constexpr
515 template < typename CharT >
516 constexpr unsigned long
517 strlen_c(const CharT *const s) noexcept
520 for (auto p = s; *p; ++p)
525 static_assert(strlen_c("") == 0UL, "");
526 static_assert(strlen_c("x") == 1UL, "");
527 static_assert(strlen_c("test") == 4UL, "");
528 static_assert(strlen_c("another\0test") == 7UL, "");
532 namespace test_lambda_init_capture
539 const auto lambda1 = [a = x](int b){ return a + b; };
540 const auto lambda2 = [a = lambda1(x)](){ return a; };
546 namespace test_digit_separators
549 constexpr auto ten_million = 100'000'000;
550 static_assert(ten_million == 100000000, "");
554 namespace test_return_type_deduction
557 auto f(int& x) { return x; }
558 decltype(auto) g(int& x) { return x; }
560 template < typename T1, typename T2 >
563 static constexpr auto value = false;
566 template < typename T >
569 static constexpr auto value = true;
576 static_assert(is_same<int, decltype(f(x))>::value, "");
577 static_assert(is_same<int&, decltype(g(x))>::value, "");
585 #endif // __cplusplus >= 201402L
590 dnl Tests for new features in C++17
592 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_17], [[
594 // If the compiler admits that it is not ready for C++17, why torture it?
595 // Hopefully, this will speed up the test.
599 #error "This is not a C++ compiler"
601 #elif __cplusplus <= 201402L
603 #error "This is not a C++17 compiler"
607 #if defined(__clang__)
610 #if defined(__GNUC__)
615 #include <initializer_list>
617 #include <type_traits>
622 #if !defined(REALLY_CLANG)
623 namespace test_constexpr_lambdas
626 // TODO: test it with clang++ from git
628 constexpr int foo = [](){return 42;}();
631 #endif // !defined(REALLY_CLANG)
633 namespace test::nested_namespace::definitions
638 namespace test_fold_expression
641 template<typename... Args>
642 int multiply(Args... args)
644 return (args * ... * 1);
647 template<typename... Args>
648 bool all(Args... args)
650 return (args && ...);
655 namespace test_extended_static_assert
658 static_assert (true);
662 namespace test_auto_brace_init_list
668 static_assert(std::is_same<std::initializer_list<int>, decltype(foo)>::value);
669 static_assert(std::is_same<int, decltype(bar)>::value);
672 namespace test_typename_in_template_template_parameter
675 template<template<typename> typename X> struct D;
679 namespace test_fallthrough_nodiscard_maybe_unused_attributes
687 [[nodiscard]] int f2()
689 [[maybe_unused]] auto unused = f1();
704 namespace test_extended_aggregate_initialization
720 struct derived : base1, base2
725 derived d1 {{1, 2}, {}, 4}; // full initialization
726 derived d2 {{}, {}, 4}; // value-initialized bases
730 namespace test_general_range_based_for_loop
742 const int& operator* () const
759 bool operator== (const iter& i, const sentinel& s)
764 bool operator!= (const iter& i, const sentinel& s)
788 [[maybe_unused]] auto v = i;
794 namespace test_lambda_capture_asterisk_this_by_value
811 namespace test_enum_class_construction
814 enum class byte : unsigned char
821 namespace test_constexpr_if
839 namespace test_selection_statement_with_initializer
849 if (auto i = f(); i > 0)
854 switch (auto i = f(); i + 4)
866 #if !defined(REALLY_CLANG)
867 namespace test_template_argument_deduction_for_class_templates
870 // TODO: test it with clang++ from git
872 template <typename T1, typename T2>
886 [[maybe_unused]] auto p = pair{13, 42u};
890 #endif // !defined(REALLY_CLANG)
892 namespace test_non_type_auto_template_parameters
904 #if !defined(REALLY_CLANG)
905 namespace test_structured_bindings
908 // TODO: test it with clang++ from git
910 int arr[2] = { 1, 2 };
911 std::pair<int, int> pr = { 1, 2 };
913 auto f1() -> int(&)[2]
918 auto f2() -> std::pair<int, int>&
934 auto [ x1, y1 ] = f1();
935 auto& [ xr1, yr1 ] = f1();
936 auto [ x2, y2 ] = f2();
937 auto& [ xr2, yr2 ] = f2();
938 const auto [ x3, y3 ] = f3();
941 #endif // !defined(REALLY_CLANG)
943 #if !defined(REALLY_CLANG)
944 namespace test_exception_spec_type_system
947 // TODO: test it with clang++ from git
959 template<typename T1, typename T2>
963 static_assert (std::is_same_v<Good, decltype(f(g1, g2))>);
966 #endif // !defined(REALLY_CLANG)
968 namespace test_inline_variables
971 template<class T> void f(T)
974 template<class T> inline T g(T)
979 template<> inline void f<>(int)
982 template<> int g<>(int)
991 #endif // __cplusplus <= 201402L