1 # Copyright (c) 2016-2024 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', '14', '17', or '20' for
23 # the respective C++ standard version.
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 no added switch, and then 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, 2018 Krzesimir Nowak <qdlacz@gmail.com>
46 # Copyright (c) 2019 Enji Cooper <yaneurabeya@gmail.com>
47 # Copyright (c) 2020 Jason Merrill <jason@redhat.com>
48 # Copyright (c) 2021 Jörn Heusipp <osmanx@problemloesungsmaschine.de>
50 # Copying and distribution of this file, with or without modification, are
51 # permitted in any medium without royalty provided the copyright notice
52 # and this notice are preserved. This file is offered as-is, without any
57 dnl This macro is based on the code from the AX_CXX_COMPILE_STDCXX_11 macro
58 dnl (serial version number 13).
60 AC_DEFUN([AX_CXX_COMPILE_STDCXX], [dnl
61 m4_if([$1], [11], [ax_cxx_compile_alternatives="11 0x"],
62 [$1], [14], [ax_cxx_compile_alternatives="14 1y"],
63 [$1], [17], [ax_cxx_compile_alternatives="17 1z"],
64 [$1], [20], [ax_cxx_compile_alternatives="20"],
65 [m4_fatal([invalid first argument `$1' to AX_CXX_COMPILE_STDCXX])])dnl
69 [m4_fatal([invalid second argument `$2' to AX_CXX_COMPILE_STDCXX])])dnl
70 m4_if([$3], [], [ax_cxx_compile_cxx$1_required=true],
71 [$3], [mandatory], [ax_cxx_compile_cxx$1_required=true],
72 [$3], [optional], [ax_cxx_compile_cxx$1_required=false],
73 [m4_fatal([invalid third argument `$3' to AX_CXX_COMPILE_STDCXX])])
74 AC_LANG_PUSH([C++])dnl
79 AC_CACHE_CHECK(whether $CXX supports C++$1 features by default,
80 ax_cv_cxx_compile_cxx$1,
81 [AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],
82 [ax_cv_cxx_compile_cxx$1=yes],
83 [ax_cv_cxx_compile_cxx$1=no])])
84 if test x$ax_cv_cxx_compile_cxx$1 = xyes; then
88 m4_if([$2], [noext], [], [dnl
89 if test x$ac_success = xno; then
90 for alternative in ${ax_cxx_compile_alternatives}; do
91 switch="-std=gnu++${alternative}"
92 cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_$switch])
93 AC_CACHE_CHECK(whether $CXX supports C++$1 features with $switch,
97 AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],
101 if eval test x\$$cachevar = xyes; then
102 CXX_DIALECT="$switch"
104 if test -n "$CXXCPP" ; then
105 CXXCPP="$CXXCPP $switch"
113 m4_if([$2], [ext], [], [dnl
114 if test x$ac_success = xno; then
115 dnl HP's aCC needs +std=c++11 according to:
116 dnl http://h21007.www2.hp.com/portal/download/files/unprot/aCxx/PDF_Release_Notes/769149-001.pdf
117 dnl Cray's crayCC needs "-h std=c++11"
118 dnl MSVC needs -std:c++NN for C++17 and later (default is C++14)
119 for alternative in ${ax_cxx_compile_alternatives}; do
120 for switch in -std=c++${alternative} +std=c++${alternative} "-h std=c++${alternative}" MSVC; do
121 if test x"$switch" = xMSVC; then
122 dnl AS_TR_SH maps both `:` and `=` to `_` so -std:c++17 would collide
123 dnl with -std=c++17. We suffix the cache variable name with _MSVC to
125 switch=-std:c++${alternative}
126 cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_${switch}_MSVC])
128 cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_$switch])
130 AC_CACHE_CHECK(whether $CXX supports C++$1 features with $switch,
134 AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],
135 [eval $cachevar=yes],
138 if eval test x\$$cachevar = xyes; then
139 CXX_DIALECT="$switch"
141 if test -n "$CXXCPP" ; then
142 CXXCPP="$CXXCPP $switch"
148 if test x$ac_success = xyes; then
154 if test x$ax_cxx_compile_cxx$1_required = xtrue; then
155 if test x$ac_success = xno; then
156 AC_MSG_ERROR([*** A compiler with support for C++$1 language features is required.])
159 if test x$ac_success = xno; then
161 AC_MSG_NOTICE([No compiler with C++$1 support was found])
164 AC_DEFINE(HAVE_CXX$1,1,
165 [define if the compiler supports basic C++$1 syntax])
168 AC_SUBST(CXX_DIALECT)
172 dnl Test body for checking C++11 support
174 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_11],
175 _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
178 dnl Test body for checking C++14 support
180 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_14],
181 _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
182 _AX_CXX_COMPILE_STDCXX_testbody_new_in_14
185 dnl Test body for checking C++17 support
187 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_17],
188 _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
189 _AX_CXX_COMPILE_STDCXX_testbody_new_in_14
190 _AX_CXX_COMPILE_STDCXX_testbody_new_in_17
193 dnl Test body for checking C++20 support
195 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_20],
196 _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
197 _AX_CXX_COMPILE_STDCXX_testbody_new_in_14
198 _AX_CXX_COMPILE_STDCXX_testbody_new_in_17
199 _AX_CXX_COMPILE_STDCXX_testbody_new_in_20
203 dnl Tests for new features in C++11
205 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_11], [[
207 // If the compiler admits that it is not ready for C++11, why torture it?
208 // Hopefully, this will speed up the test.
212 #error "This is not a C++ compiler"
214 // MSVC always sets __cplusplus to 199711L in older versions; newer versions
215 // only set it correctly if /Zc:__cplusplus is specified as well as a
216 // /std:c++NN switch:
217 // https://devblogs.microsoft.com/cppblog/msvc-now-correctly-reports-__cplusplus/
218 #elif __cplusplus < 201103L && !defined _MSC_VER
220 #error "This is not a C++11 compiler"
227 namespace test_static_assert
230 template <typename T>
233 static_assert(sizeof(int) <= sizeof(T), "not big enough");
238 namespace test_final_override
247 struct Derived : public Base
249 virtual ~Derived() override {}
250 virtual void f() override {}
255 namespace test_double_right_angle_brackets
258 template < typename T >
261 typedef check<void> single_type;
262 typedef check<check<void>> double_type;
263 typedef check<check<check<void>>> triple_type;
264 typedef check<check<check<check<void>>>> quadruple_type;
268 namespace test_decltype
281 namespace test_type_deduction
284 template < typename T1, typename T2 >
287 static const bool value = false;
290 template < typename T >
293 static const bool value = true;
296 template < typename T1, typename T2 >
298 add(T1 a1, T2 a2) -> decltype(a1 + a2)
304 test(const int c, volatile int v)
306 static_assert(is_same<int, decltype(0)>::value == true, "");
307 static_assert(is_same<int, decltype(c)>::value == false, "");
308 static_assert(is_same<int, decltype(v)>::value == false, "");
311 auto sumi = ac + av + 'x';
312 auto sumf = ac + av + 1.0;
313 static_assert(is_same<int, decltype(ac)>::value == true, "");
314 static_assert(is_same<int, decltype(av)>::value == true, "");
315 static_assert(is_same<int, decltype(sumi)>::value == true, "");
316 static_assert(is_same<int, decltype(sumf)>::value == false, "");
317 static_assert(is_same<int, decltype(add(c, v))>::value == true, "");
318 return (sumf > 0.0) ? sumi : add(c, v);
323 namespace test_noexcept
326 int f() { return 0; }
327 int g() noexcept { return 0; }
329 static_assert(noexcept(f()) == false, "");
330 static_assert(noexcept(g()) == true, "");
334 namespace test_constexpr
337 template < typename CharT >
338 unsigned long constexpr
339 strlen_c_r(const CharT *const s, const unsigned long acc) noexcept
341 return *s ? strlen_c_r(s + 1, acc + 1) : acc;
344 template < typename CharT >
345 unsigned long constexpr
346 strlen_c(const CharT *const s) noexcept
348 return strlen_c_r(s, 0UL);
351 static_assert(strlen_c("") == 0UL, "");
352 static_assert(strlen_c("1") == 1UL, "");
353 static_assert(strlen_c("example") == 7UL, "");
354 static_assert(strlen_c("another\0example") == 7UL, "");
358 namespace test_rvalue_references
364 static constexpr int value = N;
367 answer<1> f(int&) { return answer<1>(); }
368 answer<2> f(const int&) { return answer<2>(); }
369 answer<3> f(int&&) { return answer<3>(); }
376 static_assert(decltype(f(i))::value == 1, "");
377 static_assert(decltype(f(c))::value == 2, "");
378 static_assert(decltype(f(0))::value == 3, "");
383 namespace test_uniform_initialization
388 static const int zero {};
389 static const int one {1};
392 static_assert(test::zero == 0, "");
393 static_assert(test::one == 1, "");
397 namespace test_lambdas
403 auto lambda1 = [](){};
404 auto lambda2 = lambda1;
412 auto a = [](int i, int j){ return i + j; }(1, 2);
413 auto b = []() -> int { return '0'; }();
414 auto c = [=](){ return a + b; }();
415 auto d = [&](){ return c; }();
416 auto e = [a, &b](int x) mutable {
417 const auto identity = [](int y){ return y; };
418 for (auto i = 0; i < a; ++i)
420 return x + identity(a + b);
422 return a + b + c + d + e;
428 const auto nullary = [](){ return 0; };
429 const auto unary = [](int x){ return x; };
430 using nullary_t = decltype(nullary);
431 using unary_t = decltype(unary);
432 const auto higher1st = [](nullary_t f){ return f(); };
433 const auto higher2nd = [unary](nullary_t f1){
434 return [unary, f1](unary_t f2){ return f2(unary(f1())); };
436 return higher1st(nullary) + higher2nd(nullary)(unary);
441 namespace test_variadic_templates
447 template <int N0, int... N1toN>
448 struct sum<N0, N1toN...>
450 static constexpr auto value = N0 + sum<N1toN...>::value;
456 static constexpr auto value = 0;
459 static_assert(sum<>::value == 0, "");
460 static_assert(sum<1>::value == 1, "");
461 static_assert(sum<23>::value == 23, "");
462 static_assert(sum<1, 2>::value == 3, "");
463 static_assert(sum<5, 5, 11>::value == 21, "");
464 static_assert(sum<2, 3, 5, 7, 11, 13>::value == 41, "");
468 // http://stackoverflow.com/questions/13728184/template-aliases-and-sfinae
469 // Clang 3.1 fails with headers of libstd++ 4.8.3 when using std::function
471 namespace test_template_alias_sfinae
477 using member = typename T::member_type;
483 void func(member<T>*) {}
487 void test() { func<foo>(0); }
493 #endif // __cplusplus >= 201103L
498 dnl Tests for new features in C++14
500 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_14], [[
502 // If the compiler admits that it is not ready for C++14, why torture it?
503 // Hopefully, this will speed up the test.
507 #error "This is not a C++ compiler"
509 #elif __cplusplus < 201402L && !defined _MSC_VER
511 #error "This is not a C++14 compiler"
518 namespace test_polymorphic_lambdas
524 const auto lambda = [](auto&&... args){
525 const auto istiny = [](auto x){
526 return (sizeof(x) == 1UL) ? 1 : 0;
528 const int aretiny[] = { istiny(args)... };
531 return lambda(1, 1L, 1.0f, '1');
536 namespace test_binary_literals
539 constexpr auto ivii = 0b0000000000101010;
540 static_assert(ivii == 42, "wrong value");
544 namespace test_generalized_constexpr
547 template < typename CharT >
548 constexpr unsigned long
549 strlen_c(const CharT *const s) noexcept
552 for (auto p = s; *p; ++p)
557 static_assert(strlen_c("") == 0UL, "");
558 static_assert(strlen_c("x") == 1UL, "");
559 static_assert(strlen_c("test") == 4UL, "");
560 static_assert(strlen_c("another\0test") == 7UL, "");
564 namespace test_lambda_init_capture
571 const auto lambda1 = [a = x](int b){ return a + b; };
572 const auto lambda2 = [a = lambda1(x)](){ return a; };
578 namespace test_digit_separators
581 constexpr auto ten_million = 100'000'000;
582 static_assert(ten_million == 100000000, "");
586 namespace test_return_type_deduction
589 auto f(int& x) { return x; }
590 decltype(auto) g(int& x) { return x; }
592 template < typename T1, typename T2 >
595 static constexpr auto value = false;
598 template < typename T >
601 static constexpr auto value = true;
608 static_assert(is_same<int, decltype(f(x))>::value, "");
609 static_assert(is_same<int&, decltype(g(x))>::value, "");
617 #endif // __cplusplus >= 201402L
622 dnl Tests for new features in C++17
624 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_17], [[
626 // If the compiler admits that it is not ready for C++17, why torture it?
627 // Hopefully, this will speed up the test.
631 #error "This is not a C++ compiler"
633 #elif __cplusplus < 201703L && !defined _MSC_VER
635 #error "This is not a C++17 compiler"
639 #include <initializer_list>
641 #include <type_traits>
646 namespace test_constexpr_lambdas
649 constexpr int foo = [](){return 42;}();
653 namespace test::nested_namespace::definitions
658 namespace test_fold_expression
661 template<typename... Args>
662 int multiply(Args... args)
664 return (args * ... * 1);
667 template<typename... Args>
668 bool all(Args... args)
670 return (args && ...);
675 namespace test_extended_static_assert
678 static_assert (true);
682 namespace test_auto_brace_init_list
688 static_assert(std::is_same<std::initializer_list<int>, decltype(foo)>::value);
689 static_assert(std::is_same<int, decltype(bar)>::value);
692 namespace test_typename_in_template_template_parameter
695 template<template<typename> typename X> struct D;
699 namespace test_fallthrough_nodiscard_maybe_unused_attributes
707 [[nodiscard]] int f2()
709 [[maybe_unused]] auto unused = f1();
724 namespace test_extended_aggregate_initialization
740 struct derived : base1, base2
745 derived d1 {{1, 2}, {}, 4}; // full initialization
746 derived d2 {{}, {}, 4}; // value-initialized bases
750 namespace test_general_range_based_for_loop
762 const int& operator* () const
779 bool operator== (const iter& i, const sentinel& s)
784 bool operator!= (const iter& i, const sentinel& s)
808 [[maybe_unused]] auto v = i;
814 namespace test_lambda_capture_asterisk_this_by_value
831 namespace test_enum_class_construction
834 enum class byte : unsigned char
841 namespace test_constexpr_if
859 namespace test_selection_statement_with_initializer
869 if (auto i = f(); i > 0)
874 switch (auto i = f(); i + 4)
886 namespace test_template_argument_deduction_for_class_templates
889 template <typename T1, typename T2>
903 [[maybe_unused]] auto p = pair{13, 42u};
908 namespace test_non_type_auto_template_parameters
920 namespace test_structured_bindings
923 int arr[2] = { 1, 2 };
924 std::pair<int, int> pr = { 1, 2 };
926 auto f1() -> int(&)[2]
931 auto f2() -> std::pair<int, int>&
947 auto [ x1, y1 ] = f1();
948 auto& [ xr1, yr1 ] = f1();
949 auto [ x2, y2 ] = f2();
950 auto& [ xr2, yr2 ] = f2();
951 const auto [ x3, y3 ] = f3();
955 namespace test_exception_spec_type_system
968 template<typename T1, typename T2>
972 static_assert (std::is_same_v<Good, decltype(f(g1, g2))>);
976 namespace test_inline_variables
979 template<class T> void f(T)
982 template<class T> inline T g(T)
987 template<> inline void f<>(int)
990 template<> int g<>(int)
999 #endif // __cplusplus < 201703L && !defined _MSC_VER
1004 dnl Tests for new features in C++20
1006 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_20], [[
1010 #error "This is not a C++ compiler"
1012 #elif __cplusplus < 202002L && !defined _MSC_VER
1014 #error "This is not a C++20 compiler"
1023 // As C++20 supports feature test macros in the standard, there is no
1024 // immediate need to actually test for feature availability on the
1027 } // namespace cxx20
1029 #endif // __cplusplus < 202002L && !defined _MSC_VER