ax_cxx_compile_stdcxx.m4 20 KB

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