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 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 AX_REQUIRE_DEFINED([AC_MSG_WARN])
49 AC_DEFUN([AX_CXX_COMPILE_STDCXX], [dnl
50 m4_if([$1], [11], [ax_cxx_compile_alternatives="11 0x"],
51 [$1], [14], [ax_cxx_compile_alternatives="14 1y"],
52 [$1], [17], [ax_cxx_compile_alternatives="17 1z"],
53 [m4_fatal([invalid first argument `$1' to AX_CXX_COMPILE_STDCXX])])dnl
57 [m4_fatal([invalid second argument `$2' to AX_CXX_COMPILE_STDCXX])])dnl
58 m4_if([$3], [], [ax_cxx_compile_cxx$1_required=true],
59 [$3], [mandatory], [ax_cxx_compile_cxx$1_required=true],
60 [$3], [optional], [ax_cxx_compile_cxx$1_required=false],
61 [m4_fatal([invalid third argument `$3' to AX_CXX_COMPILE_STDCXX])])
62 AC_LANG_PUSH([C++])dnl
64 AC_CACHE_CHECK(whether $CXX supports C++$1 features by default,
65 ax_cv_cxx_compile_cxx$1,
66 [AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],
67 [ax_cv_cxx_compile_cxx$1=yes],
68 [ax_cv_cxx_compile_cxx$1=no])])
69 if test x$ax_cv_cxx_compile_cxx$1 = xyes; then
73 m4_if([$2], [noext], [], [dnl
74 if test x$ac_success = xno; then
75 for alternative in ${ax_cxx_compile_alternatives}; do
76 switch="-std=gnu++${alternative}"
77 cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_$switch])
78 AC_CACHE_CHECK(whether $CXX supports C++$1 features with $switch,
82 AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],
86 if eval test x\$$cachevar = xyes; then
88 if test -n "$CXXCPP" ; then
89 CXXCPP="$CXXCPP $switch"
97 m4_if([$2], [ext], [], [dnl
98 if test x$ac_success = xno; then
99 dnl HP's aCC needs +std=c++11 according to:
100 dnl http://h21007.www2.hp.com/portal/download/files/unprot/aCxx/PDF_Release_Notes/769149-001.pdf
101 dnl Cray's crayCC needs "-h std=c++11"
102 for alternative in ${ax_cxx_compile_alternatives}; do
103 for switch in -std=c++${alternative} +std=c++${alternative} "-h std=c++${alternative}"; do
104 cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_$switch])
105 AC_CACHE_CHECK(whether $CXX supports C++$1 features with $switch,
109 AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],
110 [eval $cachevar=yes],
113 if eval test x\$$cachevar = xyes; then
115 if test -n "$CXXCPP" ; then
116 CXXCPP="$CXXCPP $switch"
122 if test x$ac_success = xyes; then
128 if test x$ax_cxx_compile_cxx$1_required = xtrue; then
129 if test x$ac_success = xno; then
130 AC_MSG_ERROR([*** A compiler with support for C++$1 language features is required.])
133 if test x$ac_success = xno; then
135 AC_MSG_NOTICE([No compiler with C++$1 support was found])
138 AC_DEFINE(HAVE_CXX$1,1,
139 [define if the compiler supports basic C++$1 syntax])
142 m4_if([$1], [17], [AC_MSG_WARN([C++17 is not yet standardized, so the checks may change in incompatible ways anytime])])
146 dnl Test body for checking C++11 support
148 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_11],
149 _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
153 dnl Test body for checking C++14 support
155 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_14],
156 _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
157 _AX_CXX_COMPILE_STDCXX_testbody_new_in_14
160 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_17],
161 _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
162 _AX_CXX_COMPILE_STDCXX_testbody_new_in_14
163 _AX_CXX_COMPILE_STDCXX_testbody_new_in_17
166 dnl Tests for new features in C++11
168 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_11], [[
170 // If the compiler admits that it is not ready for C++11, why torture it?
171 // Hopefully, this will speed up the test.
175 #error "This is not a C++ compiler"
177 #elif __cplusplus < 201103L
179 #error "This is not a C++11 compiler"
186 namespace test_static_assert
189 template <typename T>
192 static_assert(sizeof(int) <= sizeof(T), "not big enough");
197 namespace test_final_override
205 struct Derived : public Base
207 virtual void f() override {}
212 namespace test_double_right_angle_brackets
215 template < typename T >
218 typedef check<void> single_type;
219 typedef check<check<void>> double_type;
220 typedef check<check<check<void>>> triple_type;
221 typedef check<check<check<check<void>>>> quadruple_type;
225 namespace test_decltype
238 namespace test_type_deduction
241 template < typename T1, typename T2 >
244 static const bool value = false;
247 template < typename T >
250 static const bool value = true;
253 template < typename T1, typename T2 >
255 add(T1 a1, T2 a2) -> decltype(a1 + a2)
261 test(const int c, volatile int v)
263 static_assert(is_same<int, decltype(0)>::value == true, "");
264 static_assert(is_same<int, decltype(c)>::value == false, "");
265 static_assert(is_same<int, decltype(v)>::value == false, "");
268 auto sumi = ac + av + 'x';
269 auto sumf = ac + av + 1.0;
270 static_assert(is_same<int, decltype(ac)>::value == true, "");
271 static_assert(is_same<int, decltype(av)>::value == true, "");
272 static_assert(is_same<int, decltype(sumi)>::value == true, "");
273 static_assert(is_same<int, decltype(sumf)>::value == false, "");
274 static_assert(is_same<int, decltype(add(c, v))>::value == true, "");
275 return (sumf > 0.0) ? sumi : add(c, v);
280 namespace test_noexcept
283 int f() { return 0; }
284 int g() noexcept { return 0; }
286 static_assert(noexcept(f()) == false, "");
287 static_assert(noexcept(g()) == true, "");
291 namespace test_constexpr
294 template < typename CharT >
295 unsigned long constexpr
296 strlen_c_r(const CharT *const s, const unsigned long acc) noexcept
298 return *s ? strlen_c_r(s + 1, acc + 1) : acc;
301 template < typename CharT >
302 unsigned long constexpr
303 strlen_c(const CharT *const s) noexcept
305 return strlen_c_r(s, 0UL);
308 static_assert(strlen_c("") == 0UL, "");
309 static_assert(strlen_c("1") == 1UL, "");
310 static_assert(strlen_c("example") == 7UL, "");
311 static_assert(strlen_c("another\0example") == 7UL, "");
315 namespace test_rvalue_references
321 static constexpr int value = N;
324 answer<1> f(int&) { return answer<1>(); }
325 answer<2> f(const int&) { return answer<2>(); }
326 answer<3> f(int&&) { return answer<3>(); }
333 static_assert(decltype(f(i))::value == 1, "");
334 static_assert(decltype(f(c))::value == 2, "");
335 static_assert(decltype(f(0))::value == 3, "");
340 namespace test_uniform_initialization
345 static const int zero {};
346 static const int one {1};
349 static_assert(test::zero == 0, "");
350 static_assert(test::one == 1, "");
354 namespace test_lambdas
360 auto lambda1 = [](){};
361 auto lambda2 = lambda1;
369 auto a = [](int i, int j){ return i + j; }(1, 2);
370 auto b = []() -> int { return '0'; }();
371 auto c = [=](){ return a + b; }();
372 auto d = [&](){ return c; }();
373 auto e = [a, &b](int x) mutable {
374 const auto identity = [](int y){ return y; };
375 for (auto i = 0; i < a; ++i)
377 return x + identity(a + b);
379 return a + b + c + d + e;
385 const auto nullary = [](){ return 0; };
386 const auto unary = [](int x){ return x; };
387 using nullary_t = decltype(nullary);
388 using unary_t = decltype(unary);
389 const auto higher1st = [](nullary_t f){ return f(); };
390 const auto higher2nd = [unary](nullary_t f1){
391 return [unary, f1](unary_t f2){ return f2(unary(f1())); };
393 return higher1st(nullary) + higher2nd(nullary)(unary);
398 namespace test_variadic_templates
404 template <int N0, int... N1toN>
405 struct sum<N0, N1toN...>
407 static constexpr auto value = N0 + sum<N1toN...>::value;
413 static constexpr auto value = 0;
416 static_assert(sum<>::value == 0, "");
417 static_assert(sum<1>::value == 1, "");
418 static_assert(sum<23>::value == 23, "");
419 static_assert(sum<1, 2>::value == 3, "");
420 static_assert(sum<5, 5, 11>::value == 21, "");
421 static_assert(sum<2, 3, 5, 7, 11, 13>::value == 41, "");
425 // http://stackoverflow.com/questions/13728184/template-aliases-and-sfinae
426 // Clang 3.1 fails with headers of libstd++ 4.8.3 when using std::function
428 namespace test_template_alias_sfinae
434 using member = typename T::member_type;
440 void func(member<T>*) {}
444 void test() { func<foo>(0); }
450 #endif // __cplusplus >= 201103L
455 dnl Tests for new features in C++14
457 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_14], [[
459 // If the compiler admits that it is not ready for C++14, why torture it?
460 // Hopefully, this will speed up the test.
464 #error "This is not a C++ compiler"
466 #elif __cplusplus < 201402L
468 #error "This is not a C++14 compiler"
475 namespace test_polymorphic_lambdas
481 const auto lambda = [](auto&&... args){
482 const auto istiny = [](auto x){
483 return (sizeof(x) == 1UL) ? 1 : 0;
485 const int aretiny[] = { istiny(args)... };
488 return lambda(1, 1L, 1.0f, '1');
493 namespace test_binary_literals
496 constexpr auto ivii = 0b0000000000101010;
497 static_assert(ivii == 42, "wrong value");
501 namespace test_generalized_constexpr
504 template < typename CharT >
505 constexpr unsigned long
506 strlen_c(const CharT *const s) noexcept
509 for (auto p = s; *p; ++p)
514 static_assert(strlen_c("") == 0UL, "");
515 static_assert(strlen_c("x") == 1UL, "");
516 static_assert(strlen_c("test") == 4UL, "");
517 static_assert(strlen_c("another\0test") == 7UL, "");
521 namespace test_lambda_init_capture
528 const auto lambda1 = [a = x](int b){ return a + b; };
529 const auto lambda2 = [a = lambda1(x)](){ return a; };
535 namespace test_digit_separators
538 constexpr auto ten_million = 100'000'000;
539 static_assert(ten_million == 100000000, "");
543 namespace test_return_type_deduction
546 auto f(int& x) { return x; }
547 decltype(auto) g(int& x) { return x; }
549 template < typename T1, typename T2 >
552 static constexpr auto value = false;
555 template < typename T >
558 static constexpr auto value = true;
565 static_assert(is_same<int, decltype(f(x))>::value, "");
566 static_assert(is_same<int&, decltype(g(x))>::value, "");
574 #endif // __cplusplus >= 201402L
579 dnl Tests for new features in C++17
581 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_17], [[
583 // If the compiler admits that it is not ready for C++17, why torture it?
584 // Hopefully, this will speed up the test.
588 #error "This is not a C++ compiler"
590 #elif __cplusplus <= 201402L
592 #error "This is not a C++17 compiler"
596 #if defined(__clang__)
599 #if defined(__GNUC__)
604 #include <initializer_list>
606 #include <type_traits>
611 #if !defined(REALLY_CLANG)
612 namespace test_constexpr_lambdas
615 // TODO: test it with clang++ from git
617 constexpr int foo = [](){return 42;}();
620 #endif // !defined(REALLY_CLANG)
622 namespace test::nested_namespace::definitions
627 namespace test_fold_expression
630 template<typename... Args>
631 int multiply(Args... args)
633 return (args * ... * 1);
636 template<typename... Args>
637 bool all(Args... args)
639 return (args && ...);
644 namespace test_extended_static_assert
647 static_assert (true);
651 namespace test_auto_brace_init_list
657 static_assert(std::is_same<std::initializer_list<int>, decltype(foo)>::value);
658 static_assert(std::is_same<int, decltype(bar)>::value);
661 namespace test_typename_in_template_template_parameter
664 template<template<typename> typename X> struct D;
668 namespace test_fallthrough_nodiscard_maybe_unused_attributes
676 [[nodiscard]] int f2()
678 [[maybe_unused]] auto unused = f1();
693 namespace test_extended_aggregate_initialization
709 struct derived : base1, base2
714 derived d1 {{1, 2}, {}, 4}; // full initialization
715 derived d2 {{}, {}, 4}; // value-initialized bases
719 namespace test_general_range_based_for_loop
731 const int& operator* () const
748 bool operator== (const iter& i, const sentinel& s)
753 bool operator!= (const iter& i, const sentinel& s)
777 [[maybe_unused]] auto v = i;
783 namespace test_lambda_capture_asterisk_this_by_value
800 namespace test_enum_class_construction
803 enum class byte : unsigned char
810 namespace test_constexpr_if
828 namespace test_selection_statement_with_initializer
838 if (auto i = f(); i > 0)
843 switch (auto i = f(); i + 4)
855 #if !defined(REALLY_CLANG)
856 namespace test_template_argument_deduction_for_class_templates
859 // TODO: test it with clang++ from git
861 template <typename T1, typename T2>
875 [[maybe_unused]] auto p = pair{13, 42u};
879 #endif // !defined(REALLY_CLANG)
881 namespace test_non_type_auto_template_parameters
893 #if !defined(REALLY_CLANG)
894 namespace test_structured_bindings
897 // TODO: test it with clang++ from git
899 int arr[2] = { 1, 2 };
900 std::pair<int, int> pr = { 1, 2 };
902 auto f1() -> int(&)[2]
907 auto f2() -> std::pair<int, int>&
923 auto [ x1, y1 ] = f1();
924 auto& [ xr1, yr1 ] = f1();
925 auto [ x2, y2 ] = f2();
926 auto& [ xr2, yr2 ] = f2();
927 const auto [ x3, y3 ] = f3();
930 #endif // !defined(REALLY_CLANG)
932 #if !defined(REALLY_CLANG)
933 namespace test_exception_spec_type_system
936 // TODO: test it with clang++ from git
948 template<typename T1, typename T2>
952 static_assert (std::is_same_v<Good, decltype(f(g1, g2))>);
955 #endif // !defined(REALLY_CLANG)
957 namespace test_inline_variables
960 template<class T> void f(T)
963 template<class T> inline T g(T)
968 template<> inline void f<>(int)
971 template<> int g<>(int)
980 #endif // __cplusplus <= 201402L