1 # SPDX-License-Identifier: FSFAP
2 # ===========================================================================
3 # https://www.gnu.org/software/autoconf-archive/ax_cxx_compile_stdcxx.html
4 # ===========================================================================
8 # AX_CXX_COMPILE_STDCXX(VERSION, [ext|noext], [mandatory|optional])
12 # Check for baseline language coverage in the compiler for the specified
13 # version of the C++ standard. If necessary, add switches to CXX and
14 # CXXCPP to enable support. VERSION may be '11', '14', '17', or '20' for
15 # the respective C++ standard version.
17 # The second argument, if specified, indicates whether you insist on an
18 # extended mode (e.g. -std=gnu++11) or a strict conformance mode (e.g.
19 # -std=c++11). If neither is specified, you get whatever works, with
20 # preference for no added switch, and then for an extended mode.
22 # The third argument, if specified 'mandatory' or if left unspecified,
23 # indicates that baseline support for the specified C++ standard is
24 # required and that the macro should error out if no mode with that
25 # support is found. If specified 'optional', then configuration proceeds
26 # regardless, after defining HAVE_CXX${VERSION} if and only if a
27 # supporting mode is found.
31 # Copyright (c) 2008 Benjamin Kosnik <bkoz@redhat.com>
32 # Copyright (c) 2012 Zack Weinberg <zackw@panix.com>
33 # Copyright (c) 2013 Roy Stogner <roystgnr@ices.utexas.edu>
34 # Copyright (c) 2014, 2015 Google Inc.; contributed by Alexey Sokolov <sokolov@google.com>
35 # Copyright (c) 2015 Paul Norman <penorman@mac.com>
36 # Copyright (c) 2015 Moritz Klammler <moritz@klammler.eu>
37 # Copyright (c) 2016, 2018 Krzesimir Nowak <qdlacz@gmail.com>
38 # Copyright (c) 2019 Enji Cooper <yaneurabeya@gmail.com>
39 # Copyright (c) 2020 Jason Merrill <jason@redhat.com>
40 # Copyright (c) 2021 Jörn Heusipp <osmanx@problemloesungsmaschine.de>
42 # Copying and distribution of this file, with or without modification, are
43 # permitted in any medium without royalty provided the copyright notice
44 # and this notice are preserved. This file is offered as-is, without any
49 dnl This macro is based on the code from the AX_CXX_COMPILE_STDCXX_11 macro
50 dnl (serial version number 13).
52 AC_DEFUN([AX_CXX_COMPILE_STDCXX], [dnl
53 m4_if([$1], [11], [ax_cxx_compile_alternatives="11 0x"],
54 [$1], [14], [ax_cxx_compile_alternatives="14 1y"],
55 [$1], [17], [ax_cxx_compile_alternatives="17 1z"],
56 [$1], [20], [ax_cxx_compile_alternatives="20"],
57 [m4_fatal([invalid first argument `$1' to AX_CXX_COMPILE_STDCXX])])dnl
61 [m4_fatal([invalid second argument `$2' to AX_CXX_COMPILE_STDCXX])])dnl
62 m4_if([$3], [], [ax_cxx_compile_cxx$1_required=true],
63 [$3], [mandatory], [ax_cxx_compile_cxx$1_required=true],
64 [$3], [optional], [ax_cxx_compile_cxx$1_required=false],
65 [m4_fatal([invalid third argument `$3' to AX_CXX_COMPILE_STDCXX])])
66 AC_LANG_PUSH([C++])dnl
70 AC_CACHE_CHECK(whether $CXX supports C++$1 features by default,
71 ax_cv_cxx_compile_cxx$1,
72 [AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],
73 [ax_cv_cxx_compile_cxx$1=yes],
74 [ax_cv_cxx_compile_cxx$1=no])])
75 if test x$ax_cv_cxx_compile_cxx$1 = xyes; then
79 m4_if([$2], [noext], [], [dnl
80 if test x$ac_success = xno; then
81 for alternative in ${ax_cxx_compile_alternatives}; do
82 switch="-std=gnu++${alternative}"
83 cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_$switch])
84 AC_CACHE_CHECK(whether $CXX supports C++$1 features with $switch,
88 AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],
92 if eval test x\$$cachevar = xyes; then
94 if test -n "$CXXCPP" ; then
95 CXXCPP="$CXXCPP $switch"
103 m4_if([$2], [ext], [], [dnl
104 if test x$ac_success = xno; then
105 dnl HP's aCC needs +std=c++11 according to:
106 dnl http://h21007.www2.hp.com/portal/download/files/unprot/aCxx/PDF_Release_Notes/769149-001.pdf
107 dnl Cray's crayCC needs "-h std=c++11"
108 dnl MSVC needs -std:c++NN for C++17 and later (default is C++14)
109 for alternative in ${ax_cxx_compile_alternatives}; do
110 for switch in -std=c++${alternative} +std=c++${alternative} "-h std=c++${alternative}" MSVC; do
111 if test x"$switch" = xMSVC; then
112 dnl AS_TR_SH maps both `:` and `=` to `_` so -std:c++17 would collide
113 dnl with -std=c++17. We suffix the cache variable name with _MSVC to
115 switch=-std:c++${alternative}
116 cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_${switch}_MSVC])
118 cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_$switch])
120 AC_CACHE_CHECK(whether $CXX supports C++$1 features with $switch,
124 AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],
125 [eval $cachevar=yes],
128 if eval test x\$$cachevar = xyes; then
130 if test -n "$CXXCPP" ; then
131 CXXCPP="$CXXCPP $switch"
137 if test x$ac_success = xyes; then
143 if test x$ax_cxx_compile_cxx$1_required = xtrue; then
144 if test x$ac_success = xno; then
145 AC_MSG_ERROR([*** A compiler with support for C++$1 language features is required.])
148 if test x$ac_success = xno; then
150 AC_MSG_NOTICE([No compiler with C++$1 support was found])
153 AC_DEFINE(HAVE_CXX$1,1,
154 [define if the compiler supports basic C++$1 syntax])
160 dnl Test body for checking C++11 support
162 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_11],
163 _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
166 dnl Test body for checking C++14 support
168 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_14],
169 _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
170 _AX_CXX_COMPILE_STDCXX_testbody_new_in_14
173 dnl Test body for checking C++17 support
175 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_17],
176 _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
177 _AX_CXX_COMPILE_STDCXX_testbody_new_in_14
178 _AX_CXX_COMPILE_STDCXX_testbody_new_in_17
181 dnl Test body for checking C++20 support
183 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_20],
184 _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
185 _AX_CXX_COMPILE_STDCXX_testbody_new_in_14
186 _AX_CXX_COMPILE_STDCXX_testbody_new_in_17
187 _AX_CXX_COMPILE_STDCXX_testbody_new_in_20
191 dnl Tests for new features in C++11
193 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_11], [[
195 // If the compiler admits that it is not ready for C++11, why torture it?
196 // Hopefully, this will speed up the test.
200 #error "This is not a C++ compiler"
202 // MSVC always sets __cplusplus to 199711L in older versions; newer versions
203 // only set it correctly if /Zc:__cplusplus is specified as well as a
204 // /std:c++NN switch:
205 // https://devblogs.microsoft.com/cppblog/msvc-now-correctly-reports-__cplusplus/
206 #elif __cplusplus < 201103L && !defined _MSC_VER
208 #error "This is not a C++11 compiler"
215 namespace test_static_assert
218 template <typename T>
221 static_assert(sizeof(int) <= sizeof(T), "not big enough");
226 namespace test_final_override
235 struct Derived : public Base
237 virtual ~Derived() override {}
238 virtual void f() override {}
243 namespace test_double_right_angle_brackets
246 template < typename T >
249 typedef check<void> single_type;
250 typedef check<check<void>> double_type;
251 typedef check<check<check<void>>> triple_type;
252 typedef check<check<check<check<void>>>> quadruple_type;
256 namespace test_decltype
269 namespace test_type_deduction
272 template < typename T1, typename T2 >
275 static const bool value = false;
278 template < typename T >
281 static const bool value = true;
284 template < typename T1, typename T2 >
286 add(T1 a1, T2 a2) -> decltype(a1 + a2)
292 test(const int c, volatile int v)
294 static_assert(is_same<int, decltype(0)>::value == true, "");
295 static_assert(is_same<int, decltype(c)>::value == false, "");
296 static_assert(is_same<int, decltype(v)>::value == false, "");
299 auto sumi = ac + av + 'x';
300 auto sumf = ac + av + 1.0;
301 static_assert(is_same<int, decltype(ac)>::value == true, "");
302 static_assert(is_same<int, decltype(av)>::value == true, "");
303 static_assert(is_same<int, decltype(sumi)>::value == true, "");
304 static_assert(is_same<int, decltype(sumf)>::value == false, "");
305 static_assert(is_same<int, decltype(add(c, v))>::value == true, "");
306 return (sumf > 0.0) ? sumi : add(c, v);
311 namespace test_noexcept
314 int f() { return 0; }
315 int g() noexcept { return 0; }
317 static_assert(noexcept(f()) == false, "");
318 static_assert(noexcept(g()) == true, "");
322 namespace test_constexpr
325 template < typename CharT >
326 unsigned long constexpr
327 strlen_c_r(const CharT *const s, const unsigned long acc) noexcept
329 return *s ? strlen_c_r(s + 1, acc + 1) : acc;
332 template < typename CharT >
333 unsigned long constexpr
334 strlen_c(const CharT *const s) noexcept
336 return strlen_c_r(s, 0UL);
339 static_assert(strlen_c("") == 0UL, "");
340 static_assert(strlen_c("1") == 1UL, "");
341 static_assert(strlen_c("example") == 7UL, "");
342 static_assert(strlen_c("another\0example") == 7UL, "");
346 namespace test_rvalue_references
352 static constexpr int value = N;
355 answer<1> f(int&) { return answer<1>(); }
356 answer<2> f(const int&) { return answer<2>(); }
357 answer<3> f(int&&) { return answer<3>(); }
364 static_assert(decltype(f(i))::value == 1, "");
365 static_assert(decltype(f(c))::value == 2, "");
366 static_assert(decltype(f(0))::value == 3, "");
371 namespace test_uniform_initialization
376 static const int zero {};
377 static const int one {1};
380 static_assert(test::zero == 0, "");
381 static_assert(test::one == 1, "");
385 namespace test_lambdas
391 auto lambda1 = [](){};
392 auto lambda2 = lambda1;
400 auto a = [](int i, int j){ return i + j; }(1, 2);
401 auto b = []() -> int { return '0'; }();
402 auto c = [=](){ return a + b; }();
403 auto d = [&](){ return c; }();
404 auto e = [a, &b](int x) mutable {
405 const auto identity = [](int y){ return y; };
406 for (auto i = 0; i < a; ++i)
408 return x + identity(a + b);
410 return a + b + c + d + e;
416 const auto nullary = [](){ return 0; };
417 const auto unary = [](int x){ return x; };
418 using nullary_t = decltype(nullary);
419 using unary_t = decltype(unary);
420 const auto higher1st = [](nullary_t f){ return f(); };
421 const auto higher2nd = [unary](nullary_t f1){
422 return [unary, f1](unary_t f2){ return f2(unary(f1())); };
424 return higher1st(nullary) + higher2nd(nullary)(unary);
429 namespace test_variadic_templates
435 template <int N0, int... N1toN>
436 struct sum<N0, N1toN...>
438 static constexpr auto value = N0 + sum<N1toN...>::value;
444 static constexpr auto value = 0;
447 static_assert(sum<>::value == 0, "");
448 static_assert(sum<1>::value == 1, "");
449 static_assert(sum<23>::value == 23, "");
450 static_assert(sum<1, 2>::value == 3, "");
451 static_assert(sum<5, 5, 11>::value == 21, "");
452 static_assert(sum<2, 3, 5, 7, 11, 13>::value == 41, "");
456 // http://stackoverflow.com/questions/13728184/template-aliases-and-sfinae
457 // Clang 3.1 fails with headers of libstd++ 4.8.3 when using std::function
459 namespace test_template_alias_sfinae
465 using member = typename T::member_type;
471 void func(member<T>*) {}
475 void test() { func<foo>(0); }
481 #endif // __cplusplus >= 201103L
486 dnl Tests for new features in C++14
488 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_14], [[
490 // If the compiler admits that it is not ready for C++14, why torture it?
491 // Hopefully, this will speed up the test.
495 #error "This is not a C++ compiler"
497 #elif __cplusplus < 201402L && !defined _MSC_VER
499 #error "This is not a C++14 compiler"
506 namespace test_polymorphic_lambdas
512 const auto lambda = [](auto&&... args){
513 const auto istiny = [](auto x){
514 return (sizeof(x) == 1UL) ? 1 : 0;
516 const int aretiny[] = { istiny(args)... };
519 return lambda(1, 1L, 1.0f, '1');
524 namespace test_binary_literals
527 constexpr auto ivii = 0b0000000000101010;
528 static_assert(ivii == 42, "wrong value");
532 namespace test_generalized_constexpr
535 template < typename CharT >
536 constexpr unsigned long
537 strlen_c(const CharT *const s) noexcept
540 for (auto p = s; *p; ++p)
545 static_assert(strlen_c("") == 0UL, "");
546 static_assert(strlen_c("x") == 1UL, "");
547 static_assert(strlen_c("test") == 4UL, "");
548 static_assert(strlen_c("another\0test") == 7UL, "");
552 namespace test_lambda_init_capture
559 const auto lambda1 = [a = x](int b){ return a + b; };
560 const auto lambda2 = [a = lambda1(x)](){ return a; };
566 namespace test_digit_separators
569 constexpr auto ten_million = 100'000'000;
570 static_assert(ten_million == 100000000, "");
574 namespace test_return_type_deduction
577 auto f(int& x) { return x; }
578 decltype(auto) g(int& x) { return x; }
580 template < typename T1, typename T2 >
583 static constexpr auto value = false;
586 template < typename T >
589 static constexpr auto value = true;
596 static_assert(is_same<int, decltype(f(x))>::value, "");
597 static_assert(is_same<int&, decltype(g(x))>::value, "");
605 #endif // __cplusplus >= 201402L
610 dnl Tests for new features in C++17
612 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_17], [[
614 // If the compiler admits that it is not ready for C++17, why torture it?
615 // Hopefully, this will speed up the test.
619 #error "This is not a C++ compiler"
621 #elif __cplusplus < 201703L && !defined _MSC_VER
623 #error "This is not a C++17 compiler"
627 #include <initializer_list>
629 #include <type_traits>
634 namespace test_constexpr_lambdas
637 constexpr int foo = [](){return 42;}();
641 namespace test::nested_namespace::definitions
646 namespace test_fold_expression
649 template<typename... Args>
650 int multiply(Args... args)
652 return (args * ... * 1);
655 template<typename... Args>
656 bool all(Args... args)
658 return (args && ...);
663 namespace test_extended_static_assert
666 static_assert (true);
670 namespace test_auto_brace_init_list
676 static_assert(std::is_same<std::initializer_list<int>, decltype(foo)>::value);
677 static_assert(std::is_same<int, decltype(bar)>::value);
680 namespace test_typename_in_template_template_parameter
683 template<template<typename> typename X> struct D;
687 namespace test_fallthrough_nodiscard_maybe_unused_attributes
695 [[nodiscard]] int f2()
697 [[maybe_unused]] auto unused = f1();
712 namespace test_extended_aggregate_initialization
728 struct derived : base1, base2
733 derived d1 {{1, 2}, {}, 4}; // full initialization
734 derived d2 {{}, {}, 4}; // value-initialized bases
738 namespace test_general_range_based_for_loop
750 const int& operator* () const
767 bool operator== (const iter& i, const sentinel& s)
772 bool operator!= (const iter& i, const sentinel& s)
796 [[maybe_unused]] auto v = i;
802 namespace test_lambda_capture_asterisk_this_by_value
819 namespace test_enum_class_construction
822 enum class byte : unsigned char
829 namespace test_constexpr_if
847 namespace test_selection_statement_with_initializer
857 if (auto i = f(); i > 0)
862 switch (auto i = f(); i + 4)
874 namespace test_template_argument_deduction_for_class_templates
877 template <typename T1, typename T2>
891 [[maybe_unused]] auto p = pair{13, 42u};
896 namespace test_non_type_auto_template_parameters
908 namespace test_structured_bindings
911 int arr[2] = { 1, 2 };
912 std::pair<int, int> pr = { 1, 2 };
914 auto f1() -> int(&)[2]
919 auto f2() -> std::pair<int, int>&
935 auto [ x1, y1 ] = f1();
936 auto& [ xr1, yr1 ] = f1();
937 auto [ x2, y2 ] = f2();
938 auto& [ xr2, yr2 ] = f2();
939 const auto [ x3, y3 ] = f3();
943 namespace test_exception_spec_type_system
956 template<typename T1, typename T2>
960 static_assert (std::is_same_v<Good, decltype(f(g1, g2))>);
964 namespace test_inline_variables
967 template<class T> void f(T)
970 template<class T> inline T g(T)
975 template<> inline void f<>(int)
978 template<> int g<>(int)
987 #endif // __cplusplus < 201703L && !defined _MSC_VER
992 dnl Tests for new features in C++20
994 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_20], [[
998 #error "This is not a C++ compiler"
1000 #elif __cplusplus < 202002L && !defined _MSC_VER
1002 #error "This is not a C++20 compiler"
1011 // As C++20 supports feature test macros in the standard, there is no
1012 // immediate need to actually test for feature availability on the
1015 } // namespace cxx20
1017 #endif // __cplusplus < 202002L && !defined _MSC_VER