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