1 # Copyright (c) 2016-2019 Free Software Foundation, Inc.
3 # Originally based on the AX_CXX_COMPILE_STDCXX macro found at the url
6 # Local GDB customizations:
8 # - AC_SUBST CXX_DIALECT instead of changing CXX/CXXCPP.
10 # ===========================================================================
11 # https://www.gnu.org/software/autoconf-archive/ax_cxx_compile_stdcxx.html
12 # ===========================================================================
16 # AX_CXX_COMPILE_STDCXX(VERSION, [ext|noext], [mandatory|optional])
20 # Check for baseline language coverage in the compiler for the specified
21 # version of the C++ standard. If necessary, add switches to CXX and
22 # CXXCPP to enable support. VERSION may be '11' (for the C++11 standard)
23 # or '14' (for the C++14 standard).
25 # The second argument, if specified, indicates whether you insist on an
26 # extended mode (e.g. -std=gnu++11) or a strict conformance mode (e.g.
27 # -std=c++11). If neither is specified, you get whatever works, with
28 # preference for an extended mode.
30 # The third argument, if specified 'mandatory' or if left unspecified,
31 # indicates that baseline support for the specified C++ standard is
32 # required and that the macro should error out if no mode with that
33 # support is found. If specified 'optional', then configuration proceeds
34 # regardless, after defining HAVE_CXX${VERSION} if and only if a
35 # supporting mode is found.
39 # Copyright (c) 2008 Benjamin Kosnik <bkoz@redhat.com>
40 # Copyright (c) 2012 Zack Weinberg <zackw@panix.com>
41 # Copyright (c) 2013 Roy Stogner <roystgnr@ices.utexas.edu>
42 # Copyright (c) 2014, 2015 Google Inc.; contributed by Alexey Sokolov <sokolov@google.com>
43 # Copyright (c) 2015 Paul Norman <penorman@mac.com>
44 # Copyright (c) 2015 Moritz Klammler <moritz@klammler.eu>
45 # Copyright (c) 2016 Krzesimir Nowak <qdlacz@gmail.com>
47 # Copying and distribution of this file, with or without modification, are
48 # permitted in any medium without royalty provided the copyright notice
49 # and this notice are preserved. This file is offered as-is, without any
54 dnl This macro is based on the code from the AX_CXX_COMPILE_STDCXX_11 macro
55 dnl (serial version number 13).
57 AC_DEFUN([AX_CXX_COMPILE_STDCXX], [dnl
58 m4_if([$1], [11], [ax_cxx_compile_alternatives="11 0x"],
59 [$1], [14], [ax_cxx_compile_alternatives="14 1y"],
60 [$1], [17], [ax_cxx_compile_alternatives="17 1z"],
61 [m4_fatal([invalid first argument `$1' to AX_CXX_COMPILE_STDCXX])])dnl
65 [m4_fatal([invalid second argument `$2' to AX_CXX_COMPILE_STDCXX])])dnl
66 m4_if([$3], [], [ax_cxx_compile_cxx$1_required=true],
67 [$3], [mandatory], [ax_cxx_compile_cxx$1_required=true],
68 [$3], [optional], [ax_cxx_compile_cxx$1_required=false],
69 [m4_fatal([invalid third argument `$3' to AX_CXX_COMPILE_STDCXX])])
70 AC_LANG_PUSH([C++])dnl
73 AC_CACHE_CHECK(whether $CXX supports C++$1 features by default,
74 ax_cv_cxx_compile_cxx$1,
75 [AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],
76 [ax_cv_cxx_compile_cxx$1=yes],
77 [ax_cv_cxx_compile_cxx$1=no])])
78 if test x$ax_cv_cxx_compile_cxx$1 = xyes; then
82 m4_if([$2], [noext], [], [dnl
83 if test x$ac_success = xno; then
84 for alternative in ${ax_cxx_compile_alternatives}; do
85 switch="-std=gnu++${alternative}"
86 cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_$switch])
87 AC_CACHE_CHECK(whether $CXX supports C++$1 features with $switch,
91 AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],
95 if eval test x\$$cachevar = xyes; then
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 for alternative in ${ax_cxx_compile_alternatives}; do
109 for switch in -std=c++${alternative} +std=c++${alternative} "-h std=c++${alternative}"; do
110 cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_$switch])
111 AC_CACHE_CHECK(whether $CXX supports C++$1 features with $switch,
115 AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],
116 [eval $cachevar=yes],
119 if eval test x\$$cachevar = xyes; then
120 CXX_DIALECT="$switch"
125 if test x$ac_success = xyes; then
131 if test x$ax_cxx_compile_cxx$1_required = xtrue; then
132 if test x$ac_success = xno; then
133 AC_MSG_ERROR([*** A compiler with support for C++$1 language features is required.])
136 if test x$ac_success = xno; then
138 AC_MSG_NOTICE([No compiler with C++$1 support was found])
141 AC_DEFINE(HAVE_CXX$1,1,
142 [define if the compiler supports basic C++$1 syntax])
145 AC_SUBST(CXX_DIALECT)
149 dnl Test body for checking C++11 support
151 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_11],
152 _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 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_17],
164 _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
165 _AX_CXX_COMPILE_STDCXX_testbody_new_in_14
166 _AX_CXX_COMPILE_STDCXX_testbody_new_in_17
169 dnl Tests for new features in C++11
171 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_11], [[
173 // If the compiler admits that it is not ready for C++11, why torture it?
174 // Hopefully, this will speed up the test.
178 #error "This is not a C++ compiler"
180 #elif __cplusplus < 201103L
182 #error "This is not a C++11 compiler"
189 namespace test_static_assert
192 template <typename T>
195 static_assert(sizeof(int) <= sizeof(T), "not big enough");
200 namespace test_final_override
208 struct Derived : public Base
210 virtual void f() override {}
215 namespace test_double_right_angle_brackets
218 template < typename T >
221 typedef check<void> single_type;
222 typedef check<check<void>> double_type;
223 typedef check<check<check<void>>> triple_type;
224 typedef check<check<check<check<void>>>> quadruple_type;
228 namespace test_decltype
241 namespace test_type_deduction
244 template < typename T1, typename T2 >
247 static const bool value = false;
250 template < typename T >
253 static const bool value = true;
256 template < typename T1, typename T2 >
258 add(T1 a1, T2 a2) -> decltype(a1 + a2)
264 test(const int c, volatile int v)
266 static_assert(is_same<int, decltype(0)>::value == true, "");
267 static_assert(is_same<int, decltype(c)>::value == false, "");
268 static_assert(is_same<int, decltype(v)>::value == false, "");
271 auto sumi = ac + av + 'x';
272 auto sumf = ac + av + 1.0;
273 static_assert(is_same<int, decltype(ac)>::value == true, "");
274 static_assert(is_same<int, decltype(av)>::value == true, "");
275 static_assert(is_same<int, decltype(sumi)>::value == true, "");
276 static_assert(is_same<int, decltype(sumf)>::value == false, "");
277 static_assert(is_same<int, decltype(add(c, v))>::value == true, "");
278 return (sumf > 0.0) ? sumi : add(c, v);
283 namespace test_noexcept
286 int f() { return 0; }
287 int g() noexcept { return 0; }
289 static_assert(noexcept(f()) == false, "");
290 static_assert(noexcept(g()) == true, "");
294 namespace test_constexpr
297 template < typename CharT >
298 unsigned long constexpr
299 strlen_c_r(const CharT *const s, const unsigned long acc) noexcept
301 return *s ? strlen_c_r(s + 1, acc + 1) : acc;
304 template < typename CharT >
305 unsigned long constexpr
306 strlen_c(const CharT *const s) noexcept
308 return strlen_c_r(s, 0UL);
311 static_assert(strlen_c("") == 0UL, "");
312 static_assert(strlen_c("1") == 1UL, "");
313 static_assert(strlen_c("example") == 7UL, "");
314 static_assert(strlen_c("another\0example") == 7UL, "");
318 namespace test_rvalue_references
324 static constexpr int value = N;
327 answer<1> f(int&) { return answer<1>(); }
328 answer<2> f(const int&) { return answer<2>(); }
329 answer<3> f(int&&) { return answer<3>(); }
336 static_assert(decltype(f(i))::value == 1, "");
337 static_assert(decltype(f(c))::value == 2, "");
338 static_assert(decltype(f(0))::value == 3, "");
343 namespace test_uniform_initialization
348 static const int zero {};
349 static const int one {1};
352 static_assert(test::zero == 0, "");
353 static_assert(test::one == 1, "");
357 namespace test_lambdas
363 auto lambda1 = [](){};
364 auto lambda2 = lambda1;
372 auto a = [](int i, int j){ return i + j; }(1, 2);
373 auto b = []() -> int { return '0'; }();
374 auto c = [=](){ return a + b; }();
375 auto d = [&](){ return c; }();
376 auto e = [a, &b](int x) mutable {
377 const auto identity = [](int y){ return y; };
378 for (auto i = 0; i < a; ++i)
380 return x + identity(a + b);
382 return a + b + c + d + e;
388 const auto nullary = [](){ return 0; };
389 const auto unary = [](int x){ return x; };
390 using nullary_t = decltype(nullary);
391 using unary_t = decltype(unary);
392 const auto higher1st = [](nullary_t f){ return f(); };
393 const auto higher2nd = [unary](nullary_t f1){
394 return [unary, f1](unary_t f2){ return f2(unary(f1())); };
396 return higher1st(nullary) + higher2nd(nullary)(unary);
401 namespace test_variadic_templates
407 template <int N0, int... N1toN>
408 struct sum<N0, N1toN...>
410 static constexpr auto value = N0 + sum<N1toN...>::value;
416 static constexpr auto value = 0;
419 static_assert(sum<>::value == 0, "");
420 static_assert(sum<1>::value == 1, "");
421 static_assert(sum<23>::value == 23, "");
422 static_assert(sum<1, 2>::value == 3, "");
423 static_assert(sum<5, 5, 11>::value == 21, "");
424 static_assert(sum<2, 3, 5, 7, 11, 13>::value == 41, "");
428 // http://stackoverflow.com/questions/13728184/template-aliases-and-sfinae
429 // Clang 3.1 fails with headers of libstd++ 4.8.3 when using std::function
431 namespace test_template_alias_sfinae
437 using member = typename T::member_type;
443 void func(member<T>*) {}
447 void test() { func<foo>(0); }
453 #endif // __cplusplus >= 201103L
458 dnl Tests for new features in C++14
460 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_14], [[
462 // If the compiler admits that it is not ready for C++14, why torture it?
463 // Hopefully, this will speed up the test.
467 #error "This is not a C++ compiler"
469 #elif __cplusplus < 201402L
471 #error "This is not a C++14 compiler"
478 namespace test_polymorphic_lambdas
484 const auto lambda = [](auto&&... args){
485 const auto istiny = [](auto x){
486 return (sizeof(x) == 1UL) ? 1 : 0;
488 const int aretiny[] = { istiny(args)... };
491 return lambda(1, 1L, 1.0f, '1');
496 namespace test_binary_literals
499 constexpr auto ivii = 0b0000000000101010;
500 static_assert(ivii == 42, "wrong value");
504 namespace test_generalized_constexpr
507 template < typename CharT >
508 constexpr unsigned long
509 strlen_c(const CharT *const s) noexcept
512 for (auto p = s; *p; ++p)
517 static_assert(strlen_c("") == 0UL, "");
518 static_assert(strlen_c("x") == 1UL, "");
519 static_assert(strlen_c("test") == 4UL, "");
520 static_assert(strlen_c("another\0test") == 7UL, "");
524 namespace test_lambda_init_capture
531 const auto lambda1 = [a = x](int b){ return a + b; };
532 const auto lambda2 = [a = lambda1(x)](){ return a; };
538 namespace test_digit_separators
541 constexpr auto ten_million = 100'000'000;
542 static_assert(ten_million == 100000000, "");
546 namespace test_return_type_deduction
549 auto f(int& x) { return x; }
550 decltype(auto) g(int& x) { return x; }
552 template < typename T1, typename T2 >
555 static constexpr auto value = false;
558 template < typename T >
561 static constexpr auto value = true;
568 static_assert(is_same<int, decltype(f(x))>::value, "");
569 static_assert(is_same<int&, decltype(g(x))>::value, "");
577 #endif // __cplusplus >= 201402L
582 dnl Tests for new features in C++17
584 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_17], [[
586 // If the compiler admits that it is not ready for C++17, why torture it?
587 // Hopefully, this will speed up the test.
591 #error "This is not a C++ compiler"
593 #elif __cplusplus <= 201402L
595 #error "This is not a C++17 compiler"
599 #if defined(__clang__)
602 #if defined(__GNUC__)
607 #include <initializer_list>
609 #include <type_traits>
614 #if !defined(REALLY_CLANG)
615 namespace test_constexpr_lambdas
618 // TODO: test it with clang++ from git
620 constexpr int foo = [](){return 42;}();
623 #endif // !defined(REALLY_CLANG)
625 namespace test::nested_namespace::definitions
630 namespace test_fold_expression
633 template<typename... Args>
634 int multiply(Args... args)
636 return (args * ... * 1);
639 template<typename... Args>
640 bool all(Args... args)
642 return (args && ...);
647 namespace test_extended_static_assert
650 static_assert (true);
654 namespace test_auto_brace_init_list
660 static_assert(std::is_same<std::initializer_list<int>, decltype(foo)>::value);
661 static_assert(std::is_same<int, decltype(bar)>::value);
664 namespace test_typename_in_template_template_parameter
667 template<template<typename> typename X> struct D;
671 namespace test_fallthrough_nodiscard_maybe_unused_attributes
679 [[nodiscard]] int f2()
681 [[maybe_unused]] auto unused = f1();
696 namespace test_extended_aggregate_initialization
712 struct derived : base1, base2
717 derived d1 {{1, 2}, {}, 4}; // full initialization
718 derived d2 {{}, {}, 4}; // value-initialized bases
722 namespace test_general_range_based_for_loop
734 const int& operator* () const
751 bool operator== (const iter& i, const sentinel& s)
756 bool operator!= (const iter& i, const sentinel& s)
780 [[maybe_unused]] auto v = i;
786 namespace test_lambda_capture_asterisk_this_by_value
803 namespace test_enum_class_construction
806 enum class byte : unsigned char
813 namespace test_constexpr_if
831 namespace test_selection_statement_with_initializer
841 if (auto i = f(); i > 0)
846 switch (auto i = f(); i + 4)
858 #if !defined(REALLY_CLANG)
859 namespace test_template_argument_deduction_for_class_templates
862 // TODO: test it with clang++ from git
864 template <typename T1, typename T2>
878 [[maybe_unused]] auto p = pair{13, 42u};
882 #endif // !defined(REALLY_CLANG)
884 namespace test_non_type_auto_template_parameters
896 #if !defined(REALLY_CLANG)
897 namespace test_structured_bindings
900 // TODO: test it with clang++ from git
902 int arr[2] = { 1, 2 };
903 std::pair<int, int> pr = { 1, 2 };
905 auto f1() -> int(&)[2]
910 auto f2() -> std::pair<int, int>&
926 auto [ x1, y1 ] = f1();
927 auto& [ xr1, yr1 ] = f1();
928 auto [ x2, y2 ] = f2();
929 auto& [ xr2, yr2 ] = f2();
930 const auto [ x3, y3 ] = f3();
933 #endif // !defined(REALLY_CLANG)
935 #if !defined(REALLY_CLANG)
936 namespace test_exception_spec_type_system
939 // TODO: test it with clang++ from git
951 template<typename T1, typename T2>
955 static_assert (std::is_same_v<Good, decltype(f(g1, g2))>);
958 #endif // !defined(REALLY_CLANG)
960 namespace test_inline_variables
963 template<class T> void f(T)
966 template<class T> inline T g(T)
971 template<> inline void f<>(int)
974 template<> int g<>(int)
983 #endif // __cplusplus <= 201402L