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' (for the C++11 standard)
14 # or '14' (for the C++14 standard).
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 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>
38 # Copying and distribution of this file, with or without modification, are
39 # permitted in any medium without royalty provided the copyright notice
40 # and this notice are preserved. This file is offered as-is, without any
45 dnl This macro is based on the code from the AX_CXX_COMPILE_STDCXX_11 macro
46 dnl (serial version number 13).
48 AC_DEFUN([AX_CXX_COMPILE_STDCXX], [dnl
49 m4_if([$1], [11], [ax_cxx_compile_alternatives="11 0x"],
50 [$1], [14], [ax_cxx_compile_alternatives="14 1y"],
51 [$1], [17], [ax_cxx_compile_alternatives="17 1z"],
52 [m4_fatal([invalid first argument `$1' to AX_CXX_COMPILE_STDCXX])])dnl
56 [m4_fatal([invalid second argument `$2' to AX_CXX_COMPILE_STDCXX])])dnl
57 m4_if([$3], [], [ax_cxx_compile_cxx$1_required=true],
58 [$3], [mandatory], [ax_cxx_compile_cxx$1_required=true],
59 [$3], [optional], [ax_cxx_compile_cxx$1_required=false],
60 [m4_fatal([invalid third argument `$3' to AX_CXX_COMPILE_STDCXX])])
61 AC_LANG_PUSH([C++])dnl
64 m4_if([$2], [noext], [], [dnl
65 if test x$ac_success = xno; then
66 for alternative in ${ax_cxx_compile_alternatives}; do
67 switch="-std=gnu++${alternative}"
68 cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_$switch])
69 AC_CACHE_CHECK(whether $CXX supports C++$1 features with $switch,
73 AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],
77 if eval test x\$$cachevar = xyes; then
79 if test -n "$CXXCPP" ; then
80 CXXCPP="$CXXCPP $switch"
88 m4_if([$2], [ext], [], [dnl
89 if test x$ac_success = xno; then
90 dnl HP's aCC needs +std=c++11 according to:
91 dnl http://h21007.www2.hp.com/portal/download/files/unprot/aCxx/PDF_Release_Notes/769149-001.pdf
92 dnl Cray's crayCC needs "-h std=c++11"
93 for alternative in ${ax_cxx_compile_alternatives}; do
94 for switch in -std=c++${alternative} +std=c++${alternative} "-h std=c++${alternative}"; do
95 cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_$switch])
96 AC_CACHE_CHECK(whether $CXX supports C++$1 features with $switch,
100 AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],
101 [eval $cachevar=yes],
104 if eval test x\$$cachevar = xyes; then
106 if test -n "$CXXCPP" ; then
107 CXXCPP="$CXXCPP $switch"
113 if test x$ac_success = xyes; then
119 if test x$ax_cxx_compile_cxx$1_required = xtrue; then
120 if test x$ac_success = xno; then
121 AC_MSG_ERROR([*** A compiler with support for C++$1 language features is required.])
124 if test x$ac_success = xno; then
126 AC_MSG_NOTICE([No compiler with C++$1 support was found])
129 AC_DEFINE(HAVE_CXX$1,1,
130 [define if the compiler supports basic C++$1 syntax])
136 dnl Test body for checking C++11 support
138 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_11],
139 _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
143 dnl Test body for checking C++14 support
145 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_14],
146 _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
147 _AX_CXX_COMPILE_STDCXX_testbody_new_in_14
150 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_17],
151 _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
152 _AX_CXX_COMPILE_STDCXX_testbody_new_in_14
153 _AX_CXX_COMPILE_STDCXX_testbody_new_in_17
156 dnl Tests for new features in C++11
158 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_11], [[
160 // If the compiler admits that it is not ready for C++11, why torture it?
161 // Hopefully, this will speed up the test.
165 #error "This is not a C++ compiler"
167 #elif __cplusplus < 201103L
169 #error "This is not a C++11 compiler"
176 namespace test_static_assert
179 template <typename T>
182 static_assert(sizeof(int) <= sizeof(T), "not big enough");
187 namespace test_final_override
195 struct Derived : public Base
197 virtual void f() override {}
202 namespace test_double_right_angle_brackets
205 template < typename T >
208 typedef check<void> single_type;
209 typedef check<check<void>> double_type;
210 typedef check<check<check<void>>> triple_type;
211 typedef check<check<check<check<void>>>> quadruple_type;
215 namespace test_decltype
228 namespace test_type_deduction
231 template < typename T1, typename T2 >
234 static const bool value = false;
237 template < typename T >
240 static const bool value = true;
243 template < typename T1, typename T2 >
245 add(T1 a1, T2 a2) -> decltype(a1 + a2)
251 test(const int c, volatile int v)
253 static_assert(is_same<int, decltype(0)>::value == true, "");
254 static_assert(is_same<int, decltype(c)>::value == false, "");
255 static_assert(is_same<int, decltype(v)>::value == false, "");
258 auto sumi = ac + av + 'x';
259 auto sumf = ac + av + 1.0;
260 static_assert(is_same<int, decltype(ac)>::value == true, "");
261 static_assert(is_same<int, decltype(av)>::value == true, "");
262 static_assert(is_same<int, decltype(sumi)>::value == true, "");
263 static_assert(is_same<int, decltype(sumf)>::value == false, "");
264 static_assert(is_same<int, decltype(add(c, v))>::value == true, "");
265 return (sumf > 0.0) ? sumi : add(c, v);
270 namespace test_noexcept
273 int f() { return 0; }
274 int g() noexcept { return 0; }
276 static_assert(noexcept(f()) == false, "");
277 static_assert(noexcept(g()) == true, "");
281 namespace test_constexpr
284 template < typename CharT >
285 unsigned long constexpr
286 strlen_c_r(const CharT *const s, const unsigned long acc) noexcept
288 return *s ? strlen_c_r(s + 1, acc + 1) : acc;
291 template < typename CharT >
292 unsigned long constexpr
293 strlen_c(const CharT *const s) noexcept
295 return strlen_c_r(s, 0UL);
298 static_assert(strlen_c("") == 0UL, "");
299 static_assert(strlen_c("1") == 1UL, "");
300 static_assert(strlen_c("example") == 7UL, "");
301 static_assert(strlen_c("another\0example") == 7UL, "");
305 namespace test_rvalue_references
311 static constexpr int value = N;
314 answer<1> f(int&) { return answer<1>(); }
315 answer<2> f(const int&) { return answer<2>(); }
316 answer<3> f(int&&) { return answer<3>(); }
323 static_assert(decltype(f(i))::value == 1, "");
324 static_assert(decltype(f(c))::value == 2, "");
325 static_assert(decltype(f(0))::value == 3, "");
330 namespace test_uniform_initialization
335 static const int zero {};
336 static const int one {1};
339 static_assert(test::zero == 0, "");
340 static_assert(test::one == 1, "");
344 namespace test_lambdas
350 auto lambda1 = [](){};
351 auto lambda2 = lambda1;
359 auto a = [](int i, int j){ return i + j; }(1, 2);
360 auto b = []() -> int { return '0'; }();
361 auto c = [=](){ return a + b; }();
362 auto d = [&](){ return c; }();
363 auto e = [a, &b](int x) mutable {
364 const auto identity = [](int y){ return y; };
365 for (auto i = 0; i < a; ++i)
367 return x + identity(a + b);
369 return a + b + c + d + e;
375 const auto nullary = [](){ return 0; };
376 const auto unary = [](int x){ return x; };
377 using nullary_t = decltype(nullary);
378 using unary_t = decltype(unary);
379 const auto higher1st = [](nullary_t f){ return f(); };
380 const auto higher2nd = [unary](nullary_t f1){
381 return [unary, f1](unary_t f2){ return f2(unary(f1())); };
383 return higher1st(nullary) + higher2nd(nullary)(unary);
388 namespace test_variadic_templates
394 template <int N0, int... N1toN>
395 struct sum<N0, N1toN...>
397 static constexpr auto value = N0 + sum<N1toN...>::value;
403 static constexpr auto value = 0;
406 static_assert(sum<>::value == 0, "");
407 static_assert(sum<1>::value == 1, "");
408 static_assert(sum<23>::value == 23, "");
409 static_assert(sum<1, 2>::value == 3, "");
410 static_assert(sum<5, 5, 11>::value == 21, "");
411 static_assert(sum<2, 3, 5, 7, 11, 13>::value == 41, "");
415 // http://stackoverflow.com/questions/13728184/template-aliases-and-sfinae
416 // Clang 3.1 fails with headers of libstd++ 4.8.3 when using std::function
418 namespace test_template_alias_sfinae
424 using member = typename T::member_type;
430 void func(member<T>*) {}
434 void test() { func<foo>(0); }
440 #endif // __cplusplus >= 201103L
445 dnl Tests for new features in C++14
447 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_14], [[
449 // If the compiler admits that it is not ready for C++14, why torture it?
450 // Hopefully, this will speed up the test.
454 #error "This is not a C++ compiler"
456 #elif __cplusplus < 201402L
458 #error "This is not a C++14 compiler"
465 namespace test_polymorphic_lambdas
471 const auto lambda = [](auto&&... args){
472 const auto istiny = [](auto x){
473 return (sizeof(x) == 1UL) ? 1 : 0;
475 const int aretiny[] = { istiny(args)... };
478 return lambda(1, 1L, 1.0f, '1');
483 namespace test_binary_literals
486 constexpr auto ivii = 0b0000000000101010;
487 static_assert(ivii == 42, "wrong value");
491 namespace test_generalized_constexpr
494 template < typename CharT >
495 constexpr unsigned long
496 strlen_c(const CharT *const s) noexcept
499 for (auto p = s; *p; ++p)
504 static_assert(strlen_c("") == 0UL, "");
505 static_assert(strlen_c("x") == 1UL, "");
506 static_assert(strlen_c("test") == 4UL, "");
507 static_assert(strlen_c("another\0test") == 7UL, "");
511 namespace test_lambda_init_capture
518 const auto lambda1 = [a = x](int b){ return a + b; };
519 const auto lambda2 = [a = lambda1(x)](){ return a; };
525 namespace test_digit_separators
528 constexpr auto ten_million = 100'000'000;
529 static_assert(ten_million == 100000000, "");
533 namespace test_return_type_deduction
536 auto f(int& x) { return x; }
537 decltype(auto) g(int& x) { return x; }
539 template < typename T1, typename T2 >
542 static constexpr auto value = false;
545 template < typename T >
548 static constexpr auto value = true;
555 static_assert(is_same<int, decltype(f(x))>::value, "");
556 static_assert(is_same<int&, decltype(g(x))>::value, "");
564 #endif // __cplusplus >= 201402L
569 dnl Tests for new features in C++17
571 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_17], [[
573 // If the compiler admits that it is not ready for C++17, why torture it?
574 // Hopefully, this will speed up the test.
578 #error "This is not a C++ compiler"
580 #elif __cplusplus < 201703L
582 #error "This is not a C++17 compiler"
586 #include <initializer_list>
588 #include <type_traits>
593 namespace test_constexpr_lambdas
596 constexpr int foo = [](){return 42;}();
600 namespace test::nested_namespace::definitions
605 namespace test_fold_expression
608 template<typename... Args>
609 int multiply(Args... args)
611 return (args * ... * 1);
614 template<typename... Args>
615 bool all(Args... args)
617 return (args && ...);
622 namespace test_extended_static_assert
625 static_assert (true);
629 namespace test_auto_brace_init_list
635 static_assert(std::is_same<std::initializer_list<int>, decltype(foo)>::value);
636 static_assert(std::is_same<int, decltype(bar)>::value);
639 namespace test_typename_in_template_template_parameter
642 template<template<typename> typename X> struct D;
646 namespace test_fallthrough_nodiscard_maybe_unused_attributes
654 [[nodiscard]] int f2()
656 [[maybe_unused]] auto unused = f1();
671 namespace test_extended_aggregate_initialization
687 struct derived : base1, base2
692 derived d1 {{1, 2}, {}, 4}; // full initialization
693 derived d2 {{}, {}, 4}; // value-initialized bases
697 namespace test_general_range_based_for_loop
709 const int& operator* () const
726 bool operator== (const iter& i, const sentinel& s)
731 bool operator!= (const iter& i, const sentinel& s)
755 [[maybe_unused]] auto v = i;
761 namespace test_lambda_capture_asterisk_this_by_value
778 namespace test_enum_class_construction
781 enum class byte : unsigned char
788 namespace test_constexpr_if
806 namespace test_selection_statement_with_initializer
816 if (auto i = f(); i > 0)
821 switch (auto i = f(); i + 4)
833 namespace test_template_argument_deduction_for_class_templates
836 template <typename T1, typename T2>
850 [[maybe_unused]] auto p = pair{13, 42u};
855 namespace test_non_type_auto_template_parameters
867 namespace test_structured_bindings
870 int arr[2] = { 1, 2 };
871 std::pair<int, int> pr = { 1, 2 };
873 auto f1() -> int(&)[2]
878 auto f2() -> std::pair<int, int>&
894 auto [ x1, y1 ] = f1();
895 auto& [ xr1, yr1 ] = f1();
896 auto [ x2, y2 ] = f2();
897 auto& [ xr2, yr2 ] = f2();
898 const auto [ x3, y3 ] = f3();
902 namespace test_exception_spec_type_system
915 template<typename T1, typename T2>
919 static_assert (std::is_same_v<Good, decltype(f(g1, g2))>);
923 namespace test_inline_variables
926 template<class T> void f(T)
929 template<class T> inline T g(T)
934 template<> inline void f<>(int)
937 template<> int g<>(int)
946 #endif // __cplusplus < 201703L