macros.h: Implement SIDE_PARAM_SELECT_ARG1
[libside.git] / m4 / ax_cxx_compile_stdcxx.m4
1 # ===========================================================================
2 # https://www.gnu.org/software/autoconf-archive/ax_cxx_compile_stdcxx.html
3 # ===========================================================================
4 #
5 # SYNOPSIS
6 #
7 # AX_CXX_COMPILE_STDCXX(VERSION, [ext|noext], [mandatory|optional])
8 #
9 # DESCRIPTION
10 #
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).
15 #
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.
20 #
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.
27 #
28 # LICENSE
29 #
30 # SPDX-License-Identifier: FSFAP
31 #
32 # Copyright (c) 2008 Benjamin Kosnik <bkoz@redhat.com>
33 # Copyright (c) 2012 Zack Weinberg <zackw@panix.com>
34 # Copyright (c) 2013 Roy Stogner <roystgnr@ices.utexas.edu>
35 # Copyright (c) 2014, 2015 Google Inc.; contributed by Alexey Sokolov <sokolov@google.com>
36 # Copyright (c) 2015 Paul Norman <penorman@mac.com>
37 # Copyright (c) 2015 Moritz Klammler <moritz@klammler.eu>
38 # Copyright (c) 2016, 2018 Krzesimir Nowak <qdlacz@gmail.com>
39 # Copyright (c) 2019 Enji Cooper <yaneurabeya@gmail.com>
40 #
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
44 # warranty.
45
46 #serial 11
47
48 dnl This macro is based on the code from the AX_CXX_COMPILE_STDCXX_11 macro
49 dnl (serial version number 13).
50
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 [m4_fatal([invalid first argument `$1' to AX_CXX_COMPILE_STDCXX])])dnl
56 m4_if([$2], [], [],
57 [$2], [ext], [],
58 [$2], [noext], [],
59 [m4_fatal([invalid second argument `$2' to AX_CXX_COMPILE_STDCXX])])dnl
60 m4_if([$3], [], [ax_cxx_compile_cxx$1_required=true],
61 [$3], [mandatory], [ax_cxx_compile_cxx$1_required=true],
62 [$3], [optional], [ax_cxx_compile_cxx$1_required=false],
63 [m4_fatal([invalid third argument `$3' to AX_CXX_COMPILE_STDCXX])])
64 AC_LANG_PUSH([C++])dnl
65 ac_success=no
66
67 m4_if([$2], [noext], [], [dnl
68 if test x$ac_success = xno; then
69 for alternative in ${ax_cxx_compile_alternatives}; do
70 switch="-std=gnu++${alternative}"
71 cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_$switch])
72 AC_CACHE_CHECK(whether $CXX supports C++$1 features with $switch,
73 $cachevar,
74 [ac_save_CXX="$CXX"
75 CXX="$CXX $switch"
76 AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],
77 [eval $cachevar=yes],
78 [eval $cachevar=no])
79 CXX="$ac_save_CXX"])
80 if eval test x\$$cachevar = xyes; then
81 CXX="$CXX $switch"
82 if test -n "$CXXCPP" ; then
83 CXXCPP="$CXXCPP $switch"
84 fi
85 ac_success=yes
86 break
87 fi
88 done
89 fi])
90
91 m4_if([$2], [ext], [], [dnl
92 if test x$ac_success = xno; then
93 dnl HP's aCC needs +std=c++11 according to:
94 dnl http://h21007.www2.hp.com/portal/download/files/unprot/aCxx/PDF_Release_Notes/769149-001.pdf
95 dnl Cray's crayCC needs "-h std=c++11"
96 for alternative in ${ax_cxx_compile_alternatives}; do
97 for switch in -std=c++${alternative} +std=c++${alternative} "-h std=c++${alternative}"; do
98 cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_$switch])
99 AC_CACHE_CHECK(whether $CXX supports C++$1 features with $switch,
100 $cachevar,
101 [ac_save_CXX="$CXX"
102 CXX="$CXX $switch"
103 AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],
104 [eval $cachevar=yes],
105 [eval $cachevar=no])
106 CXX="$ac_save_CXX"])
107 if eval test x\$$cachevar = xyes; then
108 CXX="$CXX $switch"
109 if test -n "$CXXCPP" ; then
110 CXXCPP="$CXXCPP $switch"
111 fi
112 ac_success=yes
113 break
114 fi
115 done
116 if test x$ac_success = xyes; then
117 break
118 fi
119 done
120 fi])
121 AC_LANG_POP([C++])
122 if test x$ax_cxx_compile_cxx$1_required = xtrue; then
123 if test x$ac_success = xno; then
124 AC_MSG_ERROR([*** A compiler with support for C++$1 language features is required.])
125 fi
126 fi
127 if test x$ac_success = xno; then
128 HAVE_CXX$1=0
129 AC_MSG_NOTICE([No compiler with C++$1 support was found])
130 else
131 HAVE_CXX$1=1
132 AC_DEFINE(HAVE_CXX$1,1,
133 [define if the compiler supports basic C++$1 syntax])
134 fi
135 AC_SUBST(HAVE_CXX$1)
136 ])
137
138
139 dnl Test body for checking C++11 support
140
141 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_11],
142 _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
143 )
144
145
146 dnl Test body for checking C++14 support
147
148 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_14],
149 _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
150 _AX_CXX_COMPILE_STDCXX_testbody_new_in_14
151 )
152
153 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_17],
154 _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
155 _AX_CXX_COMPILE_STDCXX_testbody_new_in_14
156 _AX_CXX_COMPILE_STDCXX_testbody_new_in_17
157 )
158
159 dnl Tests for new features in C++11
160
161 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_11], [[
162
163 // If the compiler admits that it is not ready for C++11, why torture it?
164 // Hopefully, this will speed up the test.
165
166 #ifndef __cplusplus
167
168 #error "This is not a C++ compiler"
169
170 #elif __cplusplus < 201103L
171
172 #error "This is not a C++11 compiler"
173
174 #else
175
176 namespace cxx11
177 {
178
179 namespace test_static_assert
180 {
181
182 template <typename T>
183 struct check
184 {
185 static_assert(sizeof(int) <= sizeof(T), "not big enough");
186 };
187
188 }
189
190 namespace test_final_override
191 {
192
193 struct Base
194 {
195 virtual ~Base() {}
196 virtual void f() {}
197 };
198
199 struct Derived : public Base
200 {
201 virtual ~Derived() override {}
202 virtual void f() override {}
203 };
204
205 }
206
207 namespace test_double_right_angle_brackets
208 {
209
210 template < typename T >
211 struct check {};
212
213 typedef check<void> single_type;
214 typedef check<check<void>> double_type;
215 typedef check<check<check<void>>> triple_type;
216 typedef check<check<check<check<void>>>> quadruple_type;
217
218 }
219
220 namespace test_decltype
221 {
222
223 int
224 f()
225 {
226 int a = 1;
227 decltype(a) b = 2;
228 return a + b;
229 }
230
231 }
232
233 namespace test_type_deduction
234 {
235
236 template < typename T1, typename T2 >
237 struct is_same
238 {
239 static const bool value = false;
240 };
241
242 template < typename T >
243 struct is_same<T, T>
244 {
245 static const bool value = true;
246 };
247
248 template < typename T1, typename T2 >
249 auto
250 add(T1 a1, T2 a2) -> decltype(a1 + a2)
251 {
252 return a1 + a2;
253 }
254
255 int
256 test(const int c, volatile int v)
257 {
258 static_assert(is_same<int, decltype(0)>::value == true, "");
259 static_assert(is_same<int, decltype(c)>::value == false, "");
260 static_assert(is_same<int, decltype(v)>::value == false, "");
261 auto ac = c;
262 auto av = v;
263 auto sumi = ac + av + 'x';
264 auto sumf = ac + av + 1.0;
265 static_assert(is_same<int, decltype(ac)>::value == true, "");
266 static_assert(is_same<int, decltype(av)>::value == true, "");
267 static_assert(is_same<int, decltype(sumi)>::value == true, "");
268 static_assert(is_same<int, decltype(sumf)>::value == false, "");
269 static_assert(is_same<int, decltype(add(c, v))>::value == true, "");
270 return (sumf > 0.0) ? sumi : add(c, v);
271 }
272
273 }
274
275 namespace test_noexcept
276 {
277
278 int f() { return 0; }
279 int g() noexcept { return 0; }
280
281 static_assert(noexcept(f()) == false, "");
282 static_assert(noexcept(g()) == true, "");
283
284 }
285
286 namespace test_constexpr
287 {
288
289 template < typename CharT >
290 unsigned long constexpr
291 strlen_c_r(const CharT *const s, const unsigned long acc) noexcept
292 {
293 return *s ? strlen_c_r(s + 1, acc + 1) : acc;
294 }
295
296 template < typename CharT >
297 unsigned long constexpr
298 strlen_c(const CharT *const s) noexcept
299 {
300 return strlen_c_r(s, 0UL);
301 }
302
303 static_assert(strlen_c("") == 0UL, "");
304 static_assert(strlen_c("1") == 1UL, "");
305 static_assert(strlen_c("example") == 7UL, "");
306 static_assert(strlen_c("another\0example") == 7UL, "");
307
308 }
309
310 namespace test_rvalue_references
311 {
312
313 template < int N >
314 struct answer
315 {
316 static constexpr int value = N;
317 };
318
319 answer<1> f(int&) { return answer<1>(); }
320 answer<2> f(const int&) { return answer<2>(); }
321 answer<3> f(int&&) { return answer<3>(); }
322
323 void
324 test()
325 {
326 int i = 0;
327 const int c = 0;
328 static_assert(decltype(f(i))::value == 1, "");
329 static_assert(decltype(f(c))::value == 2, "");
330 static_assert(decltype(f(0))::value == 3, "");
331 }
332
333 }
334
335 namespace test_uniform_initialization
336 {
337
338 struct test
339 {
340 static const int zero {};
341 static const int one {1};
342 };
343
344 static_assert(test::zero == 0, "");
345 static_assert(test::one == 1, "");
346
347 }
348
349 namespace test_lambdas
350 {
351
352 void
353 test1()
354 {
355 auto lambda1 = [](){};
356 auto lambda2 = lambda1;
357 lambda1();
358 lambda2();
359 }
360
361 int
362 test2()
363 {
364 auto a = [](int i, int j){ return i + j; }(1, 2);
365 auto b = []() -> int { return '0'; }();
366 auto c = [=](){ return a + b; }();
367 auto d = [&](){ return c; }();
368 auto e = [a, &b](int x) mutable {
369 const auto identity = [](int y){ return y; };
370 for (auto i = 0; i < a; ++i)
371 a += b--;
372 return x + identity(a + b);
373 }(0);
374 return a + b + c + d + e;
375 }
376
377 int
378 test3()
379 {
380 const auto nullary = [](){ return 0; };
381 const auto unary = [](int x){ return x; };
382 using nullary_t = decltype(nullary);
383 using unary_t = decltype(unary);
384 const auto higher1st = [](nullary_t f){ return f(); };
385 const auto higher2nd = [unary](nullary_t f1){
386 return [unary, f1](unary_t f2){ return f2(unary(f1())); };
387 };
388 return higher1st(nullary) + higher2nd(nullary)(unary);
389 }
390
391 }
392
393 namespace test_variadic_templates
394 {
395
396 template <int...>
397 struct sum;
398
399 template <int N0, int... N1toN>
400 struct sum<N0, N1toN...>
401 {
402 static constexpr auto value = N0 + sum<N1toN...>::value;
403 };
404
405 template <>
406 struct sum<>
407 {
408 static constexpr auto value = 0;
409 };
410
411 static_assert(sum<>::value == 0, "");
412 static_assert(sum<1>::value == 1, "");
413 static_assert(sum<23>::value == 23, "");
414 static_assert(sum<1, 2>::value == 3, "");
415 static_assert(sum<5, 5, 11>::value == 21, "");
416 static_assert(sum<2, 3, 5, 7, 11, 13>::value == 41, "");
417
418 }
419
420 // http://stackoverflow.com/questions/13728184/template-aliases-and-sfinae
421 // Clang 3.1 fails with headers of libstd++ 4.8.3 when using std::function
422 // because of this.
423 namespace test_template_alias_sfinae
424 {
425
426 struct foo {};
427
428 template<typename T>
429 using member = typename T::member_type;
430
431 template<typename T>
432 void func(...) {}
433
434 template<typename T>
435 void func(member<T>*) {}
436
437 void test();
438
439 void test() { func<foo>(0); }
440
441 }
442
443 } // namespace cxx11
444
445 #endif // __cplusplus >= 201103L
446
447 ]])
448
449
450 dnl Tests for new features in C++14
451
452 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_14], [[
453
454 // If the compiler admits that it is not ready for C++14, why torture it?
455 // Hopefully, this will speed up the test.
456
457 #ifndef __cplusplus
458
459 #error "This is not a C++ compiler"
460
461 #elif __cplusplus < 201402L
462
463 #error "This is not a C++14 compiler"
464
465 #else
466
467 namespace cxx14
468 {
469
470 namespace test_polymorphic_lambdas
471 {
472
473 int
474 test()
475 {
476 const auto lambda = [](auto&&... args){
477 const auto istiny = [](auto x){
478 return (sizeof(x) == 1UL) ? 1 : 0;
479 };
480 const int aretiny[] = { istiny(args)... };
481 return aretiny[0];
482 };
483 return lambda(1, 1L, 1.0f, '1');
484 }
485
486 }
487
488 namespace test_binary_literals
489 {
490
491 constexpr auto ivii = 0b0000000000101010;
492 static_assert(ivii == 42, "wrong value");
493
494 }
495
496 namespace test_generalized_constexpr
497 {
498
499 template < typename CharT >
500 constexpr unsigned long
501 strlen_c(const CharT *const s) noexcept
502 {
503 auto length = 0UL;
504 for (auto p = s; *p; ++p)
505 ++length;
506 return length;
507 }
508
509 static_assert(strlen_c("") == 0UL, "");
510 static_assert(strlen_c("x") == 1UL, "");
511 static_assert(strlen_c("test") == 4UL, "");
512 static_assert(strlen_c("another\0test") == 7UL, "");
513
514 }
515
516 namespace test_lambda_init_capture
517 {
518
519 int
520 test()
521 {
522 auto x = 0;
523 const auto lambda1 = [a = x](int b){ return a + b; };
524 const auto lambda2 = [a = lambda1(x)](){ return a; };
525 return lambda2();
526 }
527
528 }
529
530 namespace test_digit_separators
531 {
532
533 constexpr auto ten_million = 100'000'000;
534 static_assert(ten_million == 100000000, "");
535
536 }
537
538 namespace test_return_type_deduction
539 {
540
541 auto f(int& x) { return x; }
542 decltype(auto) g(int& x) { return x; }
543
544 template < typename T1, typename T2 >
545 struct is_same
546 {
547 static constexpr auto value = false;
548 };
549
550 template < typename T >
551 struct is_same<T, T>
552 {
553 static constexpr auto value = true;
554 };
555
556 int
557 test()
558 {
559 auto x = 0;
560 static_assert(is_same<int, decltype(f(x))>::value, "");
561 static_assert(is_same<int&, decltype(g(x))>::value, "");
562 return x;
563 }
564
565 }
566
567 } // namespace cxx14
568
569 #endif // __cplusplus >= 201402L
570
571 ]])
572
573
574 dnl Tests for new features in C++17
575
576 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_17], [[
577
578 // If the compiler admits that it is not ready for C++17, why torture it?
579 // Hopefully, this will speed up the test.
580
581 #ifndef __cplusplus
582
583 #error "This is not a C++ compiler"
584
585 #elif __cplusplus < 201703L
586
587 #error "This is not a C++17 compiler"
588
589 #else
590
591 #include <initializer_list>
592 #include <utility>
593 #include <type_traits>
594
595 namespace cxx17
596 {
597
598 namespace test_constexpr_lambdas
599 {
600
601 constexpr int foo = [](){return 42;}();
602
603 }
604
605 namespace test::nested_namespace::definitions
606 {
607
608 }
609
610 namespace test_fold_expression
611 {
612
613 template<typename... Args>
614 int multiply(Args... args)
615 {
616 return (args * ... * 1);
617 }
618
619 template<typename... Args>
620 bool all(Args... args)
621 {
622 return (args && ...);
623 }
624
625 }
626
627 namespace test_extended_static_assert
628 {
629
630 static_assert (true);
631
632 }
633
634 namespace test_auto_brace_init_list
635 {
636
637 auto foo = {5};
638 auto bar {5};
639
640 static_assert(std::is_same<std::initializer_list<int>, decltype(foo)>::value);
641 static_assert(std::is_same<int, decltype(bar)>::value);
642 }
643
644 namespace test_typename_in_template_template_parameter
645 {
646
647 template<template<typename> typename X> struct D;
648
649 }
650
651 namespace test_fallthrough_nodiscard_maybe_unused_attributes
652 {
653
654 int f1()
655 {
656 return 42;
657 }
658
659 [[nodiscard]] int f2()
660 {
661 [[maybe_unused]] auto unused = f1();
662
663 switch (f1())
664 {
665 case 17:
666 f1();
667 [[fallthrough]];
668 case 42:
669 f1();
670 }
671 return f1();
672 }
673
674 }
675
676 namespace test_extended_aggregate_initialization
677 {
678
679 struct base1
680 {
681 int b1, b2 = 42;
682 };
683
684 struct base2
685 {
686 base2() {
687 b3 = 42;
688 }
689 int b3;
690 };
691
692 struct derived : base1, base2
693 {
694 int d;
695 };
696
697 derived d1 {{1, 2}, {}, 4}; // full initialization
698 derived d2 {{}, {}, 4}; // value-initialized bases
699
700 }
701
702 namespace test_general_range_based_for_loop
703 {
704
705 struct iter
706 {
707 int i;
708
709 int& operator* ()
710 {
711 return i;
712 }
713
714 const int& operator* () const
715 {
716 return i;
717 }
718
719 iter& operator++()
720 {
721 ++i;
722 return *this;
723 }
724 };
725
726 struct sentinel
727 {
728 int i;
729 };
730
731 bool operator== (const iter& i, const sentinel& s)
732 {
733 return i.i == s.i;
734 }
735
736 bool operator!= (const iter& i, const sentinel& s)
737 {
738 return !(i == s);
739 }
740
741 struct range
742 {
743 iter begin() const
744 {
745 return {0};
746 }
747
748 sentinel end() const
749 {
750 return {5};
751 }
752 };
753
754 void f()
755 {
756 range r {};
757
758 for (auto i : r)
759 {
760 [[maybe_unused]] auto v = i;
761 }
762 }
763
764 }
765
766 namespace test_lambda_capture_asterisk_this_by_value
767 {
768
769 struct t
770 {
771 int i;
772 int foo()
773 {
774 return [*this]()
775 {
776 return i;
777 }();
778 }
779 };
780
781 }
782
783 namespace test_enum_class_construction
784 {
785
786 enum class byte : unsigned char
787 {};
788
789 byte foo {42};
790
791 }
792
793 namespace test_constexpr_if
794 {
795
796 template <bool cond>
797 int f ()
798 {
799 if constexpr(cond)
800 {
801 return 13;
802 }
803 else
804 {
805 return 42;
806 }
807 }
808
809 }
810
811 namespace test_selection_statement_with_initializer
812 {
813
814 int f()
815 {
816 return 13;
817 }
818
819 int f2()
820 {
821 if (auto i = f(); i > 0)
822 {
823 return 3;
824 }
825
826 switch (auto i = f(); i + 4)
827 {
828 case 17:
829 return 2;
830
831 default:
832 return 1;
833 }
834 }
835
836 }
837
838 namespace test_template_argument_deduction_for_class_templates
839 {
840
841 template <typename T1, typename T2>
842 struct pair
843 {
844 pair (T1 p1, T2 p2)
845 : m1 {p1},
846 m2 {p2}
847 {}
848
849 T1 m1;
850 T2 m2;
851 };
852
853 void f()
854 {
855 [[maybe_unused]] auto p = pair{13, 42u};
856 }
857
858 }
859
860 namespace test_non_type_auto_template_parameters
861 {
862
863 template <auto n>
864 struct B
865 {};
866
867 B<5> b1;
868 B<'a'> b2;
869
870 }
871
872 namespace test_structured_bindings
873 {
874
875 int arr[2] = { 1, 2 };
876 std::pair<int, int> pr = { 1, 2 };
877
878 auto f1() -> int(&)[2]
879 {
880 return arr;
881 }
882
883 auto f2() -> std::pair<int, int>&
884 {
885 return pr;
886 }
887
888 struct S
889 {
890 int x1 : 2;
891 volatile double y1;
892 };
893
894 S f3()
895 {
896 return {};
897 }
898
899 auto [ x1, y1 ] = f1();
900 auto& [ xr1, yr1 ] = f1();
901 auto [ x2, y2 ] = f2();
902 auto& [ xr2, yr2 ] = f2();
903 const auto [ x3, y3 ] = f3();
904
905 }
906
907 namespace test_exception_spec_type_system
908 {
909
910 struct Good {};
911 struct Bad {};
912
913 void g1() noexcept;
914 void g2();
915
916 template<typename T>
917 Bad
918 f(T*, T*);
919
920 template<typename T1, typename T2>
921 Good
922 f(T1*, T2*);
923
924 static_assert (std::is_same_v<Good, decltype(f(g1, g2))>);
925
926 }
927
928 namespace test_inline_variables
929 {
930
931 template<class T> void f(T)
932 {}
933
934 template<class T> inline T g(T)
935 {
936 return T{};
937 }
938
939 template<> inline void f<>(int)
940 {}
941
942 template<> int g<>(int)
943 {
944 return 5;
945 }
946
947 }
948
949 } // namespace cxx17
950
951 #endif // __cplusplus < 201703L
952
953 ]])
This page took 0.049734 seconds and 4 git commands to generate.