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 dnl MSVC needs -std:c++NN for C++17 and later (default is C++14)
108 for alternative in ${ax_cxx_compile_alternatives}; do
109 for switch in -std=c++${alternative} +std=c++${alternative} "-h std=c++${alternative}" MSVC; do
110 if test x"$switch" = xMSVC; then
111 dnl AS_TR_SH maps both `:` and `=` to `_` so -std:c++17 would collide
112 dnl with -std=c++17. We suffix the cache variable name with _MSVC to
114 switch=-std:c++${alternative}
115 cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_${switch}_MSVC])
117 cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_$switch])
119 AC_CACHE_CHECK(whether $CXX supports C++$1 features with $switch,
123 AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],
124 [eval $cachevar=yes],
127 if eval test x\$$cachevar = xyes; then
129 if test -n "$CXXCPP" ; then
130 CXXCPP="$CXXCPP $switch"
136 if test x$ac_success = xyes; then
142 if test x$ax_cxx_compile_cxx$1_required = xtrue; then
143 if test x$ac_success = xno; then
144 AC_MSG_ERROR([*** A compiler with support for C++$1 language features is required.])
147 if test x$ac_success = xno; then
149 AC_MSG_NOTICE([No compiler with C++$1 support was found])
152 AC_DEFINE(HAVE_CXX$1,1,
153 [define if the compiler supports basic C++$1 syntax])
159 dnl Test body for checking C++11 support
161 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_11],
162 _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
165 dnl Test body for checking C++14 support
167 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_14],
168 _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
169 _AX_CXX_COMPILE_STDCXX_testbody_new_in_14
172 dnl Test body for checking C++17 support
174 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_17],
175 _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
176 _AX_CXX_COMPILE_STDCXX_testbody_new_in_14
177 _AX_CXX_COMPILE_STDCXX_testbody_new_in_17
180 dnl Test body for checking C++20 support
182 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_20],
183 _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
184 _AX_CXX_COMPILE_STDCXX_testbody_new_in_14
185 _AX_CXX_COMPILE_STDCXX_testbody_new_in_17
186 _AX_CXX_COMPILE_STDCXX_testbody_new_in_20
190 dnl Tests for new features in C++11
192 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_11], [[
194 // If the compiler admits that it is not ready for C++11, why torture it?
195 // Hopefully, this will speed up the test.
199 #error "This is not a C++ compiler"
201 // MSVC always sets __cplusplus to 199711L in older versions; newer versions
202 // only set it correctly if /Zc:__cplusplus is specified as well as a
203 // /std:c++NN switch:
204 // https://devblogs.microsoft.com/cppblog/msvc-now-correctly-reports-__cplusplus/
205 #elif __cplusplus < 201103L && !defined _MSC_VER
207 #error "This is not a C++11 compiler"
214 namespace test_static_assert
217 template <typename T>
220 static_assert(sizeof(int) <= sizeof(T), "not big enough");
225 namespace test_final_override
234 struct Derived : public Base
236 virtual ~Derived() override {}
237 virtual void f() override {}
242 namespace test_double_right_angle_brackets
245 template < typename T >
248 typedef check<void> single_type;
249 typedef check<check<void>> double_type;
250 typedef check<check<check<void>>> triple_type;
251 typedef check<check<check<check<void>>>> quadruple_type;
255 namespace test_decltype
268 namespace test_type_deduction
271 template < typename T1, typename T2 >
274 static const bool value = false;
277 template < typename T >
280 static const bool value = true;
283 template < typename T1, typename T2 >
285 add(T1 a1, T2 a2) -> decltype(a1 + a2)
291 test(const int c, volatile int v)
293 static_assert(is_same<int, decltype(0)>::value == true, "");
294 static_assert(is_same<int, decltype(c)>::value == false, "");
295 static_assert(is_same<int, decltype(v)>::value == false, "");
298 auto sumi = ac + av + 'x';
299 auto sumf = ac + av + 1.0;
300 static_assert(is_same<int, decltype(ac)>::value == true, "");
301 static_assert(is_same<int, decltype(av)>::value == true, "");
302 static_assert(is_same<int, decltype(sumi)>::value == true, "");
303 static_assert(is_same<int, decltype(sumf)>::value == false, "");
304 static_assert(is_same<int, decltype(add(c, v))>::value == true, "");
305 return (sumf > 0.0) ? sumi : add(c, v);
310 namespace test_noexcept
313 int f() { return 0; }
314 int g() noexcept { return 0; }
316 static_assert(noexcept(f()) == false, "");
317 static_assert(noexcept(g()) == true, "");
321 namespace test_constexpr
324 template < typename CharT >
325 unsigned long constexpr
326 strlen_c_r(const CharT *const s, const unsigned long acc) noexcept
328 return *s ? strlen_c_r(s + 1, acc + 1) : acc;
331 template < typename CharT >
332 unsigned long constexpr
333 strlen_c(const CharT *const s) noexcept
335 return strlen_c_r(s, 0UL);
338 static_assert(strlen_c("") == 0UL, "");
339 static_assert(strlen_c("1") == 1UL, "");
340 static_assert(strlen_c("example") == 7UL, "");
341 static_assert(strlen_c("another\0example") == 7UL, "");
345 namespace test_rvalue_references
351 static constexpr int value = N;
354 answer<1> f(int&) { return answer<1>(); }
355 answer<2> f(const int&) { return answer<2>(); }
356 answer<3> f(int&&) { return answer<3>(); }
363 static_assert(decltype(f(i))::value == 1, "");
364 static_assert(decltype(f(c))::value == 2, "");
365 static_assert(decltype(f(0))::value == 3, "");
370 namespace test_uniform_initialization
375 static const int zero {};
376 static const int one {1};
379 static_assert(test::zero == 0, "");
380 static_assert(test::one == 1, "");
384 namespace test_lambdas
390 auto lambda1 = [](){};
391 auto lambda2 = lambda1;
399 auto a = [](int i, int j){ return i + j; }(1, 2);
400 auto b = []() -> int { return '0'; }();
401 auto c = [=](){ return a + b; }();
402 auto d = [&](){ return c; }();
403 auto e = [a, &b](int x) mutable {
404 const auto identity = [](int y){ return y; };
405 for (auto i = 0; i < a; ++i)
407 return x + identity(a + b);
409 return a + b + c + d + e;
415 const auto nullary = [](){ return 0; };
416 const auto unary = [](int x){ return x; };
417 using nullary_t = decltype(nullary);
418 using unary_t = decltype(unary);
419 const auto higher1st = [](nullary_t f){ return f(); };
420 const auto higher2nd = [unary](nullary_t f1){
421 return [unary, f1](unary_t f2){ return f2(unary(f1())); };
423 return higher1st(nullary) + higher2nd(nullary)(unary);
428 namespace test_variadic_templates
434 template <int N0, int... N1toN>
435 struct sum<N0, N1toN...>
437 static constexpr auto value = N0 + sum<N1toN...>::value;
443 static constexpr auto value = 0;
446 static_assert(sum<>::value == 0, "");
447 static_assert(sum<1>::value == 1, "");
448 static_assert(sum<23>::value == 23, "");
449 static_assert(sum<1, 2>::value == 3, "");
450 static_assert(sum<5, 5, 11>::value == 21, "");
451 static_assert(sum<2, 3, 5, 7, 11, 13>::value == 41, "");
455 // http://stackoverflow.com/questions/13728184/template-aliases-and-sfinae
456 // Clang 3.1 fails with headers of libstd++ 4.8.3 when using std::function
458 namespace test_template_alias_sfinae
464 using member = typename T::member_type;
470 void func(member<T>*) {}
474 void test() { func<foo>(0); }
480 #endif // __cplusplus >= 201103L
485 dnl Tests for new features in C++14
487 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_14], [[
489 // If the compiler admits that it is not ready for C++14, why torture it?
490 // Hopefully, this will speed up the test.
494 #error "This is not a C++ compiler"
496 #elif __cplusplus < 201402L && !defined _MSC_VER
498 #error "This is not a C++14 compiler"
505 namespace test_polymorphic_lambdas
511 const auto lambda = [](auto&&... args){
512 const auto istiny = [](auto x){
513 return (sizeof(x) == 1UL) ? 1 : 0;
515 const int aretiny[] = { istiny(args)... };
518 return lambda(1, 1L, 1.0f, '1');
523 namespace test_binary_literals
526 constexpr auto ivii = 0b0000000000101010;
527 static_assert(ivii == 42, "wrong value");
531 namespace test_generalized_constexpr
534 template < typename CharT >
535 constexpr unsigned long
536 strlen_c(const CharT *const s) noexcept
539 for (auto p = s; *p; ++p)
544 static_assert(strlen_c("") == 0UL, "");
545 static_assert(strlen_c("x") == 1UL, "");
546 static_assert(strlen_c("test") == 4UL, "");
547 static_assert(strlen_c("another\0test") == 7UL, "");
551 namespace test_lambda_init_capture
558 const auto lambda1 = [a = x](int b){ return a + b; };
559 const auto lambda2 = [a = lambda1(x)](){ return a; };
565 namespace test_digit_separators
568 constexpr auto ten_million = 100'000'000;
569 static_assert(ten_million == 100000000, "");
573 namespace test_return_type_deduction
576 auto f(int& x) { return x; }
577 decltype(auto) g(int& x) { return x; }
579 template < typename T1, typename T2 >
582 static constexpr auto value = false;
585 template < typename T >
588 static constexpr auto value = true;
595 static_assert(is_same<int, decltype(f(x))>::value, "");
596 static_assert(is_same<int&, decltype(g(x))>::value, "");
604 #endif // __cplusplus >= 201402L
609 dnl Tests for new features in C++17
611 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_17], [[
613 // If the compiler admits that it is not ready for C++17, why torture it?
614 // Hopefully, this will speed up the test.
618 #error "This is not a C++ compiler"
620 #elif __cplusplus < 201703L && !defined _MSC_VER
622 #error "This is not a C++17 compiler"
626 #include <initializer_list>
628 #include <type_traits>
633 namespace test_constexpr_lambdas
636 constexpr int foo = [](){return 42;}();
640 namespace test::nested_namespace::definitions
645 namespace test_fold_expression
648 template<typename... Args>
649 int multiply(Args... args)
651 return (args * ... * 1);
654 template<typename... Args>
655 bool all(Args... args)
657 return (args && ...);
662 namespace test_extended_static_assert
665 static_assert (true);
669 namespace test_auto_brace_init_list
675 static_assert(std::is_same<std::initializer_list<int>, decltype(foo)>::value);
676 static_assert(std::is_same<int, decltype(bar)>::value);
679 namespace test_typename_in_template_template_parameter
682 template<template<typename> typename X> struct D;
686 namespace test_fallthrough_nodiscard_maybe_unused_attributes
694 [[nodiscard]] int f2()
696 [[maybe_unused]] auto unused = f1();
711 namespace test_extended_aggregate_initialization
727 struct derived : base1, base2
732 derived d1 {{1, 2}, {}, 4}; // full initialization
733 derived d2 {{}, {}, 4}; // value-initialized bases
737 namespace test_general_range_based_for_loop
749 const int& operator* () const
766 bool operator== (const iter& i, const sentinel& s)
771 bool operator!= (const iter& i, const sentinel& s)
795 [[maybe_unused]] auto v = i;
801 namespace test_lambda_capture_asterisk_this_by_value
818 namespace test_enum_class_construction
821 enum class byte : unsigned char
828 namespace test_constexpr_if
846 namespace test_selection_statement_with_initializer
856 if (auto i = f(); i > 0)
861 switch (auto i = f(); i + 4)
873 namespace test_template_argument_deduction_for_class_templates
876 template <typename T1, typename T2>
890 [[maybe_unused]] auto p = pair{13, 42u};
895 namespace test_non_type_auto_template_parameters
907 namespace test_structured_bindings
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();
942 namespace test_exception_spec_type_system
955 template<typename T1, typename T2>
959 static_assert (std::is_same_v<Good, decltype(f(g1, g2))>);
963 namespace test_inline_variables
966 template<class T> void f(T)
969 template<class T> inline T g(T)
974 template<> inline void f<>(int)
977 template<> int g<>(int)
986 #endif // __cplusplus < 201703L && !defined _MSC_VER
991 dnl Tests for new features in C++20
993 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_20], [[
997 #error "This is not a C++ compiler"
999 #elif __cplusplus < 202002L && !defined _MSC_VER
1001 #error "This is not a C++20 compiler"
1010 // As C++20 supports feature test macros in the standard, there is no
1011 // immediate need to actually test for feature availability on the
1014 } // namespace cxx20
1016 #endif // __cplusplus < 202002L && !defined _MSC_VER