1 # ===========================================================================
2 # https://www.gnu.org/software/autoconf-archive/ax_cxx_compile_stdcxx.html
3 # ===========================================================================
7 # AX_CXX_COMPILE_STDCXX(VERSION, [ext|noext], [mandatory|optional])
11 # Check for baseline language coverage in the compiler for the specified
12 # version of the C++ standard. If necessary, add switches to CXX and
13 # CXXCPP to enable support. VERSION may be '11', '14', '17', or '20' for
14 # the respective C++ standard version.
16 # The second argument, if specified, indicates whether you insist on an
17 # extended mode (e.g. -std=gnu++11) or a strict conformance mode (e.g.
18 # -std=c++11). If neither is specified, you get whatever works, with
19 # preference for no added switch, and then for an extended mode.
21 # The third argument, if specified 'mandatory' or if left unspecified,
22 # indicates that baseline support for the specified C++ standard is
23 # required and that the macro should error out if no mode with that
24 # support is found. If specified 'optional', then configuration proceeds
25 # regardless, after defining HAVE_CXX${VERSION} if and only if a
26 # supporting mode is found.
30 # Copyright (c) 2008 Benjamin Kosnik <bkoz@redhat.com>
31 # Copyright (c) 2012 Zack Weinberg <zackw@panix.com>
32 # Copyright (c) 2013 Roy Stogner <roystgnr@ices.utexas.edu>
33 # Copyright (c) 2014, 2015 Google Inc.; contributed by Alexey Sokolov <sokolov@google.com>
34 # Copyright (c) 2015 Paul Norman <penorman@mac.com>
35 # Copyright (c) 2015 Moritz Klammler <moritz@klammler.eu>
36 # Copyright (c) 2016, 2018 Krzesimir Nowak <qdlacz@gmail.com>
37 # Copyright (c) 2019 Enji Cooper <yaneurabeya@gmail.com>
38 # Copyright (c) 2020 Jason Merrill <jason@redhat.com>
39 # Copyright (c) 2021 Jörn Heusipp <osmanx@problemloesungsmaschine.de>
41 # Copying and distribution of this file, with or without modification, are
42 # permitted in any medium without royalty provided the copyright notice
43 # and this notice are preserved. This file is offered as-is, without any
48 dnl This macro is based on the code from the AX_CXX_COMPILE_STDCXX_11 macro
49 dnl (serial version number 13).
51 AC_DEFUN([AX_CXX_COMPILE_STDCXX], [dnl
52 m4_if([$1], [11], [ax_cxx_compile_alternatives="11 0x"],
53 [$1], [14], [ax_cxx_compile_alternatives="14 1y"],
54 [$1], [17], [ax_cxx_compile_alternatives="17 1z"],
55 [$1], [20], [ax_cxx_compile_alternatives="20"],
56 [m4_fatal([invalid first argument `$1' to AX_CXX_COMPILE_STDCXX])])dnl
60 [m4_fatal([invalid second argument `$2' to AX_CXX_COMPILE_STDCXX])])dnl
61 m4_if([$3], [], [ax_cxx_compile_cxx$1_required=true],
62 [$3], [mandatory], [ax_cxx_compile_cxx$1_required=true],
63 [$3], [optional], [ax_cxx_compile_cxx$1_required=false],
64 [m4_fatal([invalid third argument `$3' to AX_CXX_COMPILE_STDCXX])])
65 AC_LANG_PUSH([C++])dnl
69 AC_CACHE_CHECK(whether $CXX supports C++$1 features by default,
70 ax_cv_cxx_compile_cxx$1,
71 [AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],
72 [ax_cv_cxx_compile_cxx$1=yes],
73 [ax_cv_cxx_compile_cxx$1=no])])
74 if test x$ax_cv_cxx_compile_cxx$1 = xyes; then
78 m4_if([$2], [noext], [], [dnl
79 if test x$ac_success = xno; then
80 for alternative in ${ax_cxx_compile_alternatives}; do
81 switch="-std=gnu++${alternative}"
82 cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_$switch])
83 AC_CACHE_CHECK(whether $CXX supports C++$1 features with $switch,
87 AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],
91 if eval test x\$$cachevar = xyes; then
93 if test -n "$CXXCPP" ; then
94 CXXCPP="$CXXCPP $switch"
102 m4_if([$2], [ext], [], [dnl
103 if test x$ac_success = xno; then
104 dnl HP's aCC needs +std=c++11 according to:
105 dnl http://h21007.www2.hp.com/portal/download/files/unprot/aCxx/PDF_Release_Notes/769149-001.pdf
106 dnl Cray's crayCC needs "-h std=c++11"
107 for alternative in ${ax_cxx_compile_alternatives}; do
108 for switch in -std=c++${alternative} +std=c++${alternative} "-h std=c++${alternative}"; do
109 cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_$switch])
110 AC_CACHE_CHECK(whether $CXX supports C++$1 features with $switch,
114 AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],
115 [eval $cachevar=yes],
118 if eval test x\$$cachevar = xyes; then
120 if test -n "$CXXCPP" ; then
121 CXXCPP="$CXXCPP $switch"
127 if test x$ac_success = xyes; then
133 if test x$ax_cxx_compile_cxx$1_required = xtrue; then
134 if test x$ac_success = xno; then
135 AC_MSG_ERROR([*** A compiler with support for C++$1 language features is required.])
138 if test x$ac_success = xno; then
140 AC_MSG_NOTICE([No compiler with C++$1 support was found])
143 AC_DEFINE(HAVE_CXX$1,1,
144 [define if the compiler supports basic C++$1 syntax])
150 dnl Test body for checking C++11 support
152 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_11],
153 _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
156 dnl Test body for checking C++14 support
158 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_14],
159 _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
160 _AX_CXX_COMPILE_STDCXX_testbody_new_in_14
163 dnl Test body for checking C++17 support
165 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_17],
166 _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
167 _AX_CXX_COMPILE_STDCXX_testbody_new_in_14
168 _AX_CXX_COMPILE_STDCXX_testbody_new_in_17
171 dnl Test body for checking C++20 support
173 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_20],
174 _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
175 _AX_CXX_COMPILE_STDCXX_testbody_new_in_14
176 _AX_CXX_COMPILE_STDCXX_testbody_new_in_17
177 _AX_CXX_COMPILE_STDCXX_testbody_new_in_20
181 dnl Tests for new features in C++11
183 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_11], [[
185 // If the compiler admits that it is not ready for C++11, why torture it?
186 // Hopefully, this will speed up the test.
190 #error "This is not a C++ compiler"
192 #elif __cplusplus < 201103L && !defined _MSC_VER
194 #error "This is not a C++11 compiler"
201 namespace test_static_assert
204 template <typename T>
207 static_assert(sizeof(int) <= sizeof(T), "not big enough");
212 namespace test_final_override
221 struct Derived : public Base
223 virtual ~Derived() override {}
224 virtual void f() override {}
229 namespace test_double_right_angle_brackets
232 template < typename T >
235 typedef check<void> single_type;
236 typedef check<check<void>> double_type;
237 typedef check<check<check<void>>> triple_type;
238 typedef check<check<check<check<void>>>> quadruple_type;
242 namespace test_decltype
255 namespace test_type_deduction
258 template < typename T1, typename T2 >
261 static const bool value = false;
264 template < typename T >
267 static const bool value = true;
270 template < typename T1, typename T2 >
272 add(T1 a1, T2 a2) -> decltype(a1 + a2)
278 test(const int c, volatile int v)
280 static_assert(is_same<int, decltype(0)>::value == true, "");
281 static_assert(is_same<int, decltype(c)>::value == false, "");
282 static_assert(is_same<int, decltype(v)>::value == false, "");
285 auto sumi = ac + av + 'x';
286 auto sumf = ac + av + 1.0;
287 static_assert(is_same<int, decltype(ac)>::value == true, "");
288 static_assert(is_same<int, decltype(av)>::value == true, "");
289 static_assert(is_same<int, decltype(sumi)>::value == true, "");
290 static_assert(is_same<int, decltype(sumf)>::value == false, "");
291 static_assert(is_same<int, decltype(add(c, v))>::value == true, "");
292 return (sumf > 0.0) ? sumi : add(c, v);
297 namespace test_noexcept
300 int f() { return 0; }
301 int g() noexcept { return 0; }
303 static_assert(noexcept(f()) == false, "");
304 static_assert(noexcept(g()) == true, "");
308 namespace test_constexpr
311 template < typename CharT >
312 unsigned long constexpr
313 strlen_c_r(const CharT *const s, const unsigned long acc) noexcept
315 return *s ? strlen_c_r(s + 1, acc + 1) : acc;
318 template < typename CharT >
319 unsigned long constexpr
320 strlen_c(const CharT *const s) noexcept
322 return strlen_c_r(s, 0UL);
325 static_assert(strlen_c("") == 0UL, "");
326 static_assert(strlen_c("1") == 1UL, "");
327 static_assert(strlen_c("example") == 7UL, "");
328 static_assert(strlen_c("another\0example") == 7UL, "");
332 namespace test_rvalue_references
338 static constexpr int value = N;
341 answer<1> f(int&) { return answer<1>(); }
342 answer<2> f(const int&) { return answer<2>(); }
343 answer<3> f(int&&) { return answer<3>(); }
350 static_assert(decltype(f(i))::value == 1, "");
351 static_assert(decltype(f(c))::value == 2, "");
352 static_assert(decltype(f(0))::value == 3, "");
357 namespace test_uniform_initialization
362 static const int zero {};
363 static const int one {1};
366 static_assert(test::zero == 0, "");
367 static_assert(test::one == 1, "");
371 namespace test_lambdas
377 auto lambda1 = [](){};
378 auto lambda2 = lambda1;
386 auto a = [](int i, int j){ return i + j; }(1, 2);
387 auto b = []() -> int { return '0'; }();
388 auto c = [=](){ return a + b; }();
389 auto d = [&](){ return c; }();
390 auto e = [a, &b](int x) mutable {
391 const auto identity = [](int y){ return y; };
392 for (auto i = 0; i < a; ++i)
394 return x + identity(a + b);
396 return a + b + c + d + e;
402 const auto nullary = [](){ return 0; };
403 const auto unary = [](int x){ return x; };
404 using nullary_t = decltype(nullary);
405 using unary_t = decltype(unary);
406 const auto higher1st = [](nullary_t f){ return f(); };
407 const auto higher2nd = [unary](nullary_t f1){
408 return [unary, f1](unary_t f2){ return f2(unary(f1())); };
410 return higher1st(nullary) + higher2nd(nullary)(unary);
415 namespace test_variadic_templates
421 template <int N0, int... N1toN>
422 struct sum<N0, N1toN...>
424 static constexpr auto value = N0 + sum<N1toN...>::value;
430 static constexpr auto value = 0;
433 static_assert(sum<>::value == 0, "");
434 static_assert(sum<1>::value == 1, "");
435 static_assert(sum<23>::value == 23, "");
436 static_assert(sum<1, 2>::value == 3, "");
437 static_assert(sum<5, 5, 11>::value == 21, "");
438 static_assert(sum<2, 3, 5, 7, 11, 13>::value == 41, "");
442 // http://stackoverflow.com/questions/13728184/template-aliases-and-sfinae
443 // Clang 3.1 fails with headers of libstd++ 4.8.3 when using std::function
445 namespace test_template_alias_sfinae
451 using member = typename T::member_type;
457 void func(member<T>*) {}
461 void test() { func<foo>(0); }
467 #endif // __cplusplus >= 201103L
472 dnl Tests for new features in C++14
474 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_14], [[
476 // If the compiler admits that it is not ready for C++14, why torture it?
477 // Hopefully, this will speed up the test.
481 #error "This is not a C++ compiler"
483 #elif __cplusplus < 201402L && !defined _MSC_VER
485 #error "This is not a C++14 compiler"
492 namespace test_polymorphic_lambdas
498 const auto lambda = [](auto&&... args){
499 const auto istiny = [](auto x){
500 return (sizeof(x) == 1UL) ? 1 : 0;
502 const int aretiny[] = { istiny(args)... };
505 return lambda(1, 1L, 1.0f, '1');
510 namespace test_binary_literals
513 constexpr auto ivii = 0b0000000000101010;
514 static_assert(ivii == 42, "wrong value");
518 namespace test_generalized_constexpr
521 template < typename CharT >
522 constexpr unsigned long
523 strlen_c(const CharT *const s) noexcept
526 for (auto p = s; *p; ++p)
531 static_assert(strlen_c("") == 0UL, "");
532 static_assert(strlen_c("x") == 1UL, "");
533 static_assert(strlen_c("test") == 4UL, "");
534 static_assert(strlen_c("another\0test") == 7UL, "");
538 namespace test_lambda_init_capture
545 const auto lambda1 = [a = x](int b){ return a + b; };
546 const auto lambda2 = [a = lambda1(x)](){ return a; };
552 namespace test_digit_separators
555 constexpr auto ten_million = 100'000'000;
556 static_assert(ten_million == 100000000, "");
560 namespace test_return_type_deduction
563 auto f(int& x) { return x; }
564 decltype(auto) g(int& x) { return x; }
566 template < typename T1, typename T2 >
569 static constexpr auto value = false;
572 template < typename T >
575 static constexpr auto value = true;
582 static_assert(is_same<int, decltype(f(x))>::value, "");
583 static_assert(is_same<int&, decltype(g(x))>::value, "");
591 #endif // __cplusplus >= 201402L
596 dnl Tests for new features in C++17
598 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_17], [[
600 // If the compiler admits that it is not ready for C++17, why torture it?
601 // Hopefully, this will speed up the test.
605 #error "This is not a C++ compiler"
607 #elif __cplusplus < 201703L && !defined _MSC_VER
609 #error "This is not a C++17 compiler"
613 #include <initializer_list>
615 #include <type_traits>
620 namespace test_constexpr_lambdas
623 constexpr int foo = [](){return 42;}();
627 namespace test::nested_namespace::definitions
632 namespace test_fold_expression
635 template<typename... Args>
636 int multiply(Args... args)
638 return (args * ... * 1);
641 template<typename... Args>
642 bool all(Args... args)
644 return (args && ...);
649 namespace test_extended_static_assert
652 static_assert (true);
656 namespace test_auto_brace_init_list
662 static_assert(std::is_same<std::initializer_list<int>, decltype(foo)>::value);
663 static_assert(std::is_same<int, decltype(bar)>::value);
666 namespace test_typename_in_template_template_parameter
669 template<template<typename> typename X> struct D;
673 namespace test_fallthrough_nodiscard_maybe_unused_attributes
681 [[nodiscard]] int f2()
683 [[maybe_unused]] auto unused = f1();
698 namespace test_extended_aggregate_initialization
714 struct derived : base1, base2
719 derived d1 {{1, 2}, {}, 4}; // full initialization
720 derived d2 {{}, {}, 4}; // value-initialized bases
724 namespace test_general_range_based_for_loop
736 const int& operator* () const
753 bool operator== (const iter& i, const sentinel& s)
758 bool operator!= (const iter& i, const sentinel& s)
782 [[maybe_unused]] auto v = i;
788 namespace test_lambda_capture_asterisk_this_by_value
805 namespace test_enum_class_construction
808 enum class byte : unsigned char
815 namespace test_constexpr_if
833 namespace test_selection_statement_with_initializer
843 if (auto i = f(); i > 0)
848 switch (auto i = f(); i + 4)
860 namespace test_template_argument_deduction_for_class_templates
863 template <typename T1, typename T2>
877 [[maybe_unused]] auto p = pair{13, 42u};
882 namespace test_non_type_auto_template_parameters
894 namespace test_structured_bindings
897 int arr[2] = { 1, 2 };
898 std::pair<int, int> pr = { 1, 2 };
900 auto f1() -> int(&)[2]
905 auto f2() -> std::pair<int, int>&
921 auto [ x1, y1 ] = f1();
922 auto& [ xr1, yr1 ] = f1();
923 auto [ x2, y2 ] = f2();
924 auto& [ xr2, yr2 ] = f2();
925 const auto [ x3, y3 ] = f3();
929 namespace test_exception_spec_type_system
942 template<typename T1, typename T2>
946 static_assert (std::is_same_v<Good, decltype(f(g1, g2))>);
950 namespace test_inline_variables
953 template<class T> void f(T)
956 template<class T> inline T g(T)
961 template<> inline void f<>(int)
964 template<> int g<>(int)
973 #endif // __cplusplus < 201703L && !defined _MSC_VER
978 dnl Tests for new features in C++20
980 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_20], [[
984 #error "This is not a C++ compiler"
986 #elif __cplusplus < 202002L && !defined _MSC_VER
988 #error "This is not a C++20 compiler"
997 // As C++20 supports feature test macros in the standard, there is no
998 // immediate need to actually test for feature availability on the
1001 } // namespace cxx20
1003 #endif // __cplusplus < 202002L && !defined _MSC_VER