ax_cxx_compile_stdcxx.m4 21 KB

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