ax_cxx_compile_stdcxx.m4 21 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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', '14', '17', or '20' for
  14. # the respective C++ standard version.
  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 no added switch, and then 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. # 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>
  37. # Copyright (c) 2019 Enji Cooper <yaneurabeya@gmail.com>
  38. # Copyright (c) 2020 Jason Merrill <jason@redhat.com>
  39. # Copyright (c) 2021 Jörn Heusipp <osmanx@problemloesungsmaschine.de>
  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. #serial 15
  46. dnl This macro is based on the code from the AX_CXX_COMPILE_STDCXX_11 macro
  47. 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. [$1], [20], [ax_cxx_compile_alternatives="20"],
  53. [m4_fatal([invalid first argument `$1' to AX_CXX_COMPILE_STDCXX])])dnl
  54. m4_if([$2], [], [],
  55. [$2], [ext], [],
  56. [$2], [noext], [],
  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
  63. ac_success=no
  64. m4_if([$2], [], [dnl
  65. AC_CACHE_CHECK(whether $CXX supports C++$1 features by default,
  66. ax_cv_cxx_compile_cxx$1,
  67. [AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],
  68. [ax_cv_cxx_compile_cxx$1=yes],
  69. [ax_cv_cxx_compile_cxx$1=no])])
  70. if test x$ax_cv_cxx_compile_cxx$1 = xyes; then
  71. ac_success=yes
  72. fi])
  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,
  79. $cachevar,
  80. [ac_save_CXX="$CXX"
  81. CXX="$CXX $switch"
  82. AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],
  83. [eval $cachevar=yes],
  84. [eval $cachevar=no])
  85. CXX="$ac_save_CXX"])
  86. if eval test x\$$cachevar = xyes; then
  87. CXX="$CXX $switch"
  88. if test -n "$CXXCPP" ; then
  89. CXXCPP="$CXXCPP $switch"
  90. fi
  91. ac_success=yes
  92. break
  93. fi
  94. done
  95. fi])
  96. m4_if([$2], [ext], [], [dnl
  97. if test x$ac_success = xno; then
  98. dnl HP's aCC needs +std=c++11 according to:
  99. dnl http://h21007.www2.hp.com/portal/download/files/unprot/aCxx/PDF_Release_Notes/769149-001.pdf
  100. dnl Cray's crayCC needs "-h std=c++11"
  101. for alternative in ${ax_cxx_compile_alternatives}; do
  102. for switch in -std=c++${alternative} +std=c++${alternative} "-h std=c++${alternative}"; do
  103. cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_$switch])
  104. AC_CACHE_CHECK(whether $CXX supports C++$1 features with $switch,
  105. $cachevar,
  106. [ac_save_CXX="$CXX"
  107. CXX="$CXX $switch"
  108. AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],
  109. [eval $cachevar=yes],
  110. [eval $cachevar=no])
  111. CXX="$ac_save_CXX"])
  112. if eval test x\$$cachevar = xyes; then
  113. CXX="$CXX $switch"
  114. if test -n "$CXXCPP" ; then
  115. CXXCPP="$CXXCPP $switch"
  116. fi
  117. ac_success=yes
  118. break
  119. fi
  120. done
  121. if test x$ac_success = xyes; then
  122. break
  123. fi
  124. done
  125. fi])
  126. AC_LANG_POP([C++])
  127. if test x$ax_cxx_compile_cxx$1_required = xtrue; then
  128. if test x$ac_success = xno; then
  129. AC_MSG_ERROR([*** A compiler with support for C++$1 language features is required.])
  130. fi
  131. fi
  132. if test x$ac_success = xno; then
  133. HAVE_CXX$1=0
  134. AC_MSG_NOTICE([No compiler with C++$1 support was found])
  135. else
  136. HAVE_CXX$1=1
  137. AC_DEFINE(HAVE_CXX$1,1,
  138. [define if the compiler supports basic C++$1 syntax])
  139. fi
  140. AC_SUBST(HAVE_CXX$1)
  141. ])
  142. dnl Test body for checking C++11 support
  143. m4_define([_AX_CXX_COMPILE_STDCXX_testbody_11],
  144. _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
  145. )
  146. dnl Test body for checking C++14 support
  147. m4_define([_AX_CXX_COMPILE_STDCXX_testbody_14],
  148. _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
  149. _AX_CXX_COMPILE_STDCXX_testbody_new_in_14
  150. )
  151. dnl Test body for checking C++17 support
  152. m4_define([_AX_CXX_COMPILE_STDCXX_testbody_17],
  153. _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
  154. _AX_CXX_COMPILE_STDCXX_testbody_new_in_14
  155. _AX_CXX_COMPILE_STDCXX_testbody_new_in_17
  156. )
  157. dnl Test body for checking C++20 support
  158. m4_define([_AX_CXX_COMPILE_STDCXX_testbody_20],
  159. _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
  160. _AX_CXX_COMPILE_STDCXX_testbody_new_in_14
  161. _AX_CXX_COMPILE_STDCXX_testbody_new_in_17
  162. _AX_CXX_COMPILE_STDCXX_testbody_new_in_20
  163. )
  164. dnl Tests for new features in C++11
  165. m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_11], [[
  166. // If the compiler admits that it is not ready for C++11, why torture it?
  167. // Hopefully, this will speed up the test.
  168. #ifndef __cplusplus
  169. #error "This is not a C++ compiler"
  170. // MSVC always sets __cplusplus to 199711L in older versions; newer versions
  171. // only set it correctly if /Zc:__cplusplus is specified as well as a
  172. // /std:c++NN switch:
  173. // https://devblogs.microsoft.com/cppblog/msvc-now-correctly-reports-__cplusplus/
  174. #elif __cplusplus < 201103L && !defined _MSC_VER
  175. #error "This is not a C++11 compiler"
  176. #else
  177. namespace cxx11
  178. {
  179. namespace test_static_assert
  180. {
  181. template <typename T>
  182. struct check
  183. {
  184. static_assert(sizeof(int) <= sizeof(T), "not big enough");
  185. };
  186. }
  187. namespace test_final_override
  188. {
  189. struct Base
  190. {
  191. virtual ~Base() {}
  192. virtual void f() {}
  193. };
  194. struct Derived : public Base
  195. {
  196. virtual ~Derived() override {}
  197. virtual void f() override {}
  198. };
  199. }
  200. namespace test_double_right_angle_brackets
  201. {
  202. template < typename T >
  203. struct check {};
  204. typedef check<void> single_type;
  205. typedef check<check<void>> double_type;
  206. typedef check<check<check<void>>> triple_type;
  207. typedef check<check<check<check<void>>>> quadruple_type;
  208. }
  209. namespace test_decltype
  210. {
  211. int
  212. f()
  213. {
  214. int a = 1;
  215. decltype(a) b = 2;
  216. return a + b;
  217. }
  218. }
  219. namespace test_type_deduction
  220. {
  221. template < typename T1, typename T2 >
  222. struct is_same
  223. {
  224. static const bool value = false;
  225. };
  226. template < typename T >
  227. struct is_same<T, T>
  228. {
  229. static const bool value = true;
  230. };
  231. template < typename T1, typename T2 >
  232. auto
  233. add(T1 a1, T2 a2) -> decltype(a1 + a2)
  234. {
  235. return a1 + a2;
  236. }
  237. int
  238. test(const int c, volatile int v)
  239. {
  240. static_assert(is_same<int, decltype(0)>::value == true, "");
  241. static_assert(is_same<int, decltype(c)>::value == false, "");
  242. static_assert(is_same<int, decltype(v)>::value == false, "");
  243. auto ac = c;
  244. auto av = v;
  245. auto sumi = ac + av + 'x';
  246. auto sumf = ac + av + 1.0;
  247. static_assert(is_same<int, decltype(ac)>::value == true, "");
  248. static_assert(is_same<int, decltype(av)>::value == true, "");
  249. static_assert(is_same<int, decltype(sumi)>::value == true, "");
  250. static_assert(is_same<int, decltype(sumf)>::value == false, "");
  251. static_assert(is_same<int, decltype(add(c, v))>::value == true, "");
  252. return (sumf > 0.0) ? sumi : add(c, v);
  253. }
  254. }
  255. namespace test_noexcept
  256. {
  257. int f() { return 0; }
  258. int g() noexcept { return 0; }
  259. static_assert(noexcept(f()) == false, "");
  260. static_assert(noexcept(g()) == true, "");
  261. }
  262. namespace test_constexpr
  263. {
  264. template < typename CharT >
  265. unsigned long constexpr
  266. strlen_c_r(const CharT *const s, const unsigned long acc) noexcept
  267. {
  268. return *s ? strlen_c_r(s + 1, acc + 1) : acc;
  269. }
  270. template < typename CharT >
  271. unsigned long constexpr
  272. strlen_c(const CharT *const s) noexcept
  273. {
  274. return strlen_c_r(s, 0UL);
  275. }
  276. static_assert(strlen_c("") == 0UL, "");
  277. static_assert(strlen_c("1") == 1UL, "");
  278. static_assert(strlen_c("example") == 7UL, "");
  279. static_assert(strlen_c("another\0example") == 7UL, "");
  280. }
  281. namespace test_rvalue_references
  282. {
  283. template < int N >
  284. struct answer
  285. {
  286. static constexpr int value = N;
  287. };
  288. answer<1> f(int&) { return answer<1>(); }
  289. answer<2> f(const int&) { return answer<2>(); }
  290. answer<3> f(int&&) { return answer<3>(); }
  291. void
  292. test()
  293. {
  294. int i = 0;
  295. const int c = 0;
  296. static_assert(decltype(f(i))::value == 1, "");
  297. static_assert(decltype(f(c))::value == 2, "");
  298. static_assert(decltype(f(0))::value == 3, "");
  299. }
  300. }
  301. namespace test_uniform_initialization
  302. {
  303. struct test
  304. {
  305. static const int zero {};
  306. static const int one {1};
  307. };
  308. static_assert(test::zero == 0, "");
  309. static_assert(test::one == 1, "");
  310. }
  311. namespace test_lambdas
  312. {
  313. void
  314. test1()
  315. {
  316. auto lambda1 = [](){};
  317. auto lambda2 = lambda1;
  318. lambda1();
  319. lambda2();
  320. }
  321. int
  322. test2()
  323. {
  324. auto a = [](int i, int j){ return i + j; }(1, 2);
  325. auto b = []() -> int { return '0'; }();
  326. auto c = [=](){ return a + b; }();
  327. auto d = [&](){ return c; }();
  328. auto e = [a, &b](int x) mutable {
  329. const auto identity = [](int y){ return y; };
  330. for (auto i = 0; i < a; ++i)
  331. a += b--;
  332. return x + identity(a + b);
  333. }(0);
  334. return a + b + c + d + e;
  335. }
  336. int
  337. test3()
  338. {
  339. const auto nullary = [](){ return 0; };
  340. const auto unary = [](int x){ return x; };
  341. using nullary_t = decltype(nullary);
  342. using unary_t = decltype(unary);
  343. const auto higher1st = [](nullary_t f){ return f(); };
  344. const auto higher2nd = [unary](nullary_t f1){
  345. return [unary, f1](unary_t f2){ return f2(unary(f1())); };
  346. };
  347. return higher1st(nullary) + higher2nd(nullary)(unary);
  348. }
  349. }
  350. namespace test_variadic_templates
  351. {
  352. template <int...>
  353. struct sum;
  354. template <int N0, int... N1toN>
  355. struct sum<N0, N1toN...>
  356. {
  357. static constexpr auto value = N0 + sum<N1toN...>::value;
  358. };
  359. template <>
  360. struct sum<>
  361. {
  362. static constexpr auto value = 0;
  363. };
  364. static_assert(sum<>::value == 0, "");
  365. static_assert(sum<1>::value == 1, "");
  366. static_assert(sum<23>::value == 23, "");
  367. static_assert(sum<1, 2>::value == 3, "");
  368. static_assert(sum<5, 5, 11>::value == 21, "");
  369. static_assert(sum<2, 3, 5, 7, 11, 13>::value == 41, "");
  370. }
  371. // http://stackoverflow.com/questions/13728184/template-aliases-and-sfinae
  372. // Clang 3.1 fails with headers of libstd++ 4.8.3 when using std::function
  373. // because of this.
  374. namespace test_template_alias_sfinae
  375. {
  376. struct foo {};
  377. template<typename T>
  378. using member = typename T::member_type;
  379. template<typename T>
  380. void func(...) {}
  381. template<typename T>
  382. void func(member<T>*) {}
  383. void test();
  384. void test() { func<foo>(0); }
  385. }
  386. } // namespace cxx11
  387. #endif // __cplusplus >= 201103L
  388. ]])
  389. dnl Tests for new features in C++14
  390. m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_14], [[
  391. // If the compiler admits that it is not ready for C++14, why torture it?
  392. // Hopefully, this will speed up the test.
  393. #ifndef __cplusplus
  394. #error "This is not a C++ compiler"
  395. #elif __cplusplus < 201402L && !defined _MSC_VER
  396. #error "This is not a C++14 compiler"
  397. #else
  398. namespace cxx14
  399. {
  400. namespace test_polymorphic_lambdas
  401. {
  402. int
  403. test()
  404. {
  405. const auto lambda = [](auto&&... args){
  406. const auto istiny = [](auto x){
  407. return (sizeof(x) == 1UL) ? 1 : 0;
  408. };
  409. const int aretiny[] = { istiny(args)... };
  410. return aretiny[0];
  411. };
  412. return lambda(1, 1L, 1.0f, '1');
  413. }
  414. }
  415. namespace test_binary_literals
  416. {
  417. constexpr auto ivii = 0b0000000000101010;
  418. static_assert(ivii == 42, "wrong value");
  419. }
  420. namespace test_generalized_constexpr
  421. {
  422. template < typename CharT >
  423. constexpr unsigned long
  424. strlen_c(const CharT *const s) noexcept
  425. {
  426. auto length = 0UL;
  427. for (auto p = s; *p; ++p)
  428. ++length;
  429. return length;
  430. }
  431. static_assert(strlen_c("") == 0UL, "");
  432. static_assert(strlen_c("x") == 1UL, "");
  433. static_assert(strlen_c("test") == 4UL, "");
  434. static_assert(strlen_c("another\0test") == 7UL, "");
  435. }
  436. namespace test_lambda_init_capture
  437. {
  438. int
  439. test()
  440. {
  441. auto x = 0;
  442. const auto lambda1 = [a = x](int b){ return a + b; };
  443. const auto lambda2 = [a = lambda1(x)](){ return a; };
  444. return lambda2();
  445. }
  446. }
  447. namespace test_digit_separators
  448. {
  449. constexpr auto ten_million = 100'000'000;
  450. static_assert(ten_million == 100000000, "");
  451. }
  452. namespace test_return_type_deduction
  453. {
  454. auto f(int& x) { return x; }
  455. decltype(auto) g(int& x) { return x; }
  456. template < typename T1, typename T2 >
  457. struct is_same
  458. {
  459. static constexpr auto value = false;
  460. };
  461. template < typename T >
  462. struct is_same<T, T>
  463. {
  464. static constexpr auto value = true;
  465. };
  466. int
  467. test()
  468. {
  469. auto x = 0;
  470. static_assert(is_same<int, decltype(f(x))>::value, "");
  471. static_assert(is_same<int&, decltype(g(x))>::value, "");
  472. return x;
  473. }
  474. }
  475. } // namespace cxx14
  476. #endif // __cplusplus >= 201402L
  477. ]])
  478. dnl Tests for new features in C++17
  479. m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_17], [[
  480. // If the compiler admits that it is not ready for C++17, why torture it?
  481. // Hopefully, this will speed up the test.
  482. #ifndef __cplusplus
  483. #error "This is not a C++ compiler"
  484. #elif __cplusplus < 201703L && !defined _MSC_VER
  485. #error "This is not a C++17 compiler"
  486. #else
  487. #include <initializer_list>
  488. #include <utility>
  489. #include <type_traits>
  490. namespace cxx17
  491. {
  492. namespace test_constexpr_lambdas
  493. {
  494. constexpr int foo = [](){return 42;}();
  495. }
  496. namespace test::nested_namespace::definitions
  497. {
  498. }
  499. namespace test_fold_expression
  500. {
  501. template<typename... Args>
  502. int multiply(Args... args)
  503. {
  504. return (args * ... * 1);
  505. }
  506. template<typename... Args>
  507. bool all(Args... args)
  508. {
  509. return (args && ...);
  510. }
  511. }
  512. namespace test_extended_static_assert
  513. {
  514. static_assert (true);
  515. }
  516. namespace test_auto_brace_init_list
  517. {
  518. auto foo = {5};
  519. auto bar {5};
  520. static_assert(std::is_same<std::initializer_list<int>, decltype(foo)>::value);
  521. static_assert(std::is_same<int, decltype(bar)>::value);
  522. }
  523. namespace test_typename_in_template_template_parameter
  524. {
  525. template<template<typename> typename X> struct D;
  526. }
  527. namespace test_fallthrough_nodiscard_maybe_unused_attributes
  528. {
  529. int f1()
  530. {
  531. return 42;
  532. }
  533. [[nodiscard]] int f2()
  534. {
  535. [[maybe_unused]] auto unused = f1();
  536. switch (f1())
  537. {
  538. case 17:
  539. f1();
  540. [[fallthrough]];
  541. case 42:
  542. f1();
  543. }
  544. return f1();
  545. }
  546. }
  547. namespace test_extended_aggregate_initialization
  548. {
  549. struct base1
  550. {
  551. int b1, b2 = 42;
  552. };
  553. struct base2
  554. {
  555. base2() {
  556. b3 = 42;
  557. }
  558. int b3;
  559. };
  560. struct derived : base1, base2
  561. {
  562. int d;
  563. };
  564. derived d1 {{1, 2}, {}, 4}; // full initialization
  565. derived d2 {{}, {}, 4}; // value-initialized bases
  566. }
  567. namespace test_general_range_based_for_loop
  568. {
  569. struct iter
  570. {
  571. int i;
  572. int& operator* ()
  573. {
  574. return i;
  575. }
  576. const int& operator* () const
  577. {
  578. return i;
  579. }
  580. iter& operator++()
  581. {
  582. ++i;
  583. return *this;
  584. }
  585. };
  586. struct sentinel
  587. {
  588. int i;
  589. };
  590. bool operator== (const iter& i, const sentinel& s)
  591. {
  592. return i.i == s.i;
  593. }
  594. bool operator!= (const iter& i, const sentinel& s)
  595. {
  596. return !(i == s);
  597. }
  598. struct range
  599. {
  600. iter begin() const
  601. {
  602. return {0};
  603. }
  604. sentinel end() const
  605. {
  606. return {5};
  607. }
  608. };
  609. void f()
  610. {
  611. range r {};
  612. for (auto i : r)
  613. {
  614. [[maybe_unused]] auto v = i;
  615. }
  616. }
  617. }
  618. namespace test_lambda_capture_asterisk_this_by_value
  619. {
  620. struct t
  621. {
  622. int i;
  623. int foo()
  624. {
  625. return [*this]()
  626. {
  627. return i;
  628. }();
  629. }
  630. };
  631. }
  632. namespace test_enum_class_construction
  633. {
  634. enum class byte : unsigned char
  635. {};
  636. byte foo {42};
  637. }
  638. namespace test_constexpr_if
  639. {
  640. template <bool cond>
  641. int f ()
  642. {
  643. if constexpr(cond)
  644. {
  645. return 13;
  646. }
  647. else
  648. {
  649. return 42;
  650. }
  651. }
  652. }
  653. namespace test_selection_statement_with_initializer
  654. {
  655. int f()
  656. {
  657. return 13;
  658. }
  659. int f2()
  660. {
  661. if (auto i = f(); i > 0)
  662. {
  663. return 3;
  664. }
  665. switch (auto i = f(); i + 4)
  666. {
  667. case 17:
  668. return 2;
  669. default:
  670. return 1;
  671. }
  672. }
  673. }
  674. namespace test_template_argument_deduction_for_class_templates
  675. {
  676. template <typename T1, typename T2>
  677. struct pair
  678. {
  679. pair (T1 p1, T2 p2)
  680. : m1 {p1},
  681. m2 {p2}
  682. {}
  683. T1 m1;
  684. T2 m2;
  685. };
  686. void f()
  687. {
  688. [[maybe_unused]] auto p = pair{13, 42u};
  689. }
  690. }
  691. namespace test_non_type_auto_template_parameters
  692. {
  693. template <auto n>
  694. struct B
  695. {};
  696. B<5> b1;
  697. B<'a'> b2;
  698. }
  699. namespace test_structured_bindings
  700. {
  701. int arr[2] = { 1, 2 };
  702. std::pair<int, int> pr = { 1, 2 };
  703. auto f1() -> int(&)[2]
  704. {
  705. return arr;
  706. }
  707. auto f2() -> std::pair<int, int>&
  708. {
  709. return pr;
  710. }
  711. struct S
  712. {
  713. int x1 : 2;
  714. volatile double y1;
  715. };
  716. S f3()
  717. {
  718. return {};
  719. }
  720. auto [ x1, y1 ] = f1();
  721. auto& [ xr1, yr1 ] = f1();
  722. auto [ x2, y2 ] = f2();
  723. auto& [ xr2, yr2 ] = f2();
  724. const auto [ x3, y3 ] = f3();
  725. }
  726. namespace test_exception_spec_type_system
  727. {
  728. struct Good {};
  729. struct Bad {};
  730. void g1() noexcept;
  731. void g2();
  732. template<typename T>
  733. Bad
  734. f(T*, T*);
  735. template<typename T1, typename T2>
  736. Good
  737. f(T1*, T2*);
  738. static_assert (std::is_same_v<Good, decltype(f(g1, g2))>);
  739. }
  740. namespace test_inline_variables
  741. {
  742. template<class T> void f(T)
  743. {}
  744. template<class T> inline T g(T)
  745. {
  746. return T{};
  747. }
  748. template<> inline void f<>(int)
  749. {}
  750. template<> int g<>(int)
  751. {
  752. return 5;
  753. }
  754. }
  755. } // namespace cxx17
  756. #endif // __cplusplus < 201703L && !defined _MSC_VER
  757. ]])
  758. dnl Tests for new features in C++20
  759. m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_20], [[
  760. #ifndef __cplusplus
  761. #error "This is not a C++ compiler"
  762. #elif __cplusplus < 202002L && !defined _MSC_VER
  763. #error "This is not a C++20 compiler"
  764. #else
  765. #include <version>
  766. namespace cxx20
  767. {
  768. // As C++20 supports feature test macros in the standard, there is no
  769. // immediate need to actually test for feature availability on the
  770. // Autoconf side.
  771. } // namespace cxx20
  772. #endif // __cplusplus < 202002L && !defined _MSC_VER
  773. ]])