ax_cxx_compile_stdcxx.m4 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  1. # Copyright (c) 2016-2022 Free Software Foundation, Inc.
  2. #
  3. # Originally based on the AX_CXX_COMPILE_STDCXX macro found at the url
  4. # below.
  5. #
  6. # Local GDB customizations:
  7. #
  8. # - AC_SUBST CXX_DIALECT instead of changing CXX/CXXCPP.
  9. #
  10. # ===========================================================================
  11. # https://www.gnu.org/software/autoconf-archive/ax_cxx_compile_stdcxx.html
  12. # ===========================================================================
  13. #
  14. # SYNOPSIS
  15. #
  16. # AX_CXX_COMPILE_STDCXX(VERSION, [ext|noext], [mandatory|optional])
  17. #
  18. # DESCRIPTION
  19. #
  20. # Check for baseline language coverage in the compiler for the specified
  21. # version of the C++ standard. If necessary, add switches to CXX and
  22. # CXXCPP to enable support. VERSION may be '11' (for the C++11 standard)
  23. # or '14' (for the C++14 standard).
  24. #
  25. # The second argument, if specified, indicates whether you insist on an
  26. # extended mode (e.g. -std=gnu++11) or a strict conformance mode (e.g.
  27. # -std=c++11). If neither is specified, you get whatever works, with
  28. # preference for an extended mode.
  29. #
  30. # The third argument, if specified 'mandatory' or if left unspecified,
  31. # indicates that baseline support for the specified C++ standard is
  32. # required and that the macro should error out if no mode with that
  33. # support is found. If specified 'optional', then configuration proceeds
  34. # regardless, after defining HAVE_CXX${VERSION} if and only if a
  35. # supporting mode is found.
  36. #
  37. # LICENSE
  38. #
  39. # Copyright (c) 2008 Benjamin Kosnik <bkoz@redhat.com>
  40. # Copyright (c) 2012 Zack Weinberg <zackw@panix.com>
  41. # Copyright (c) 2013 Roy Stogner <roystgnr@ices.utexas.edu>
  42. # Copyright (c) 2014, 2015 Google Inc.; contributed by Alexey Sokolov <sokolov@google.com>
  43. # Copyright (c) 2015 Paul Norman <penorman@mac.com>
  44. # Copyright (c) 2015 Moritz Klammler <moritz@klammler.eu>
  45. # Copyright (c) 2016 Krzesimir Nowak <qdlacz@gmail.com>
  46. #
  47. # Copying and distribution of this file, with or without modification, are
  48. # permitted in any medium without royalty provided the copyright notice
  49. # and this notice are preserved. This file is offered as-is, without any
  50. # warranty.
  51. #serial 8
  52. dnl This macro is based on the code from the AX_CXX_COMPILE_STDCXX_11 macro
  53. dnl (serial version number 13).
  54. AC_DEFUN([AX_CXX_COMPILE_STDCXX], [dnl
  55. m4_if([$1], [11], [ax_cxx_compile_alternatives="11 0x"],
  56. [$1], [14], [ax_cxx_compile_alternatives="14 1y"],
  57. [$1], [17], [ax_cxx_compile_alternatives="17 1z"],
  58. [m4_fatal([invalid first argument `$1' to AX_CXX_COMPILE_STDCXX])])dnl
  59. m4_if([$2], [], [],
  60. [$2], [ext], [],
  61. [$2], [noext], [],
  62. [m4_fatal([invalid second argument `$2' to AX_CXX_COMPILE_STDCXX])])dnl
  63. m4_if([$3], [], [ax_cxx_compile_cxx$1_required=true],
  64. [$3], [mandatory], [ax_cxx_compile_cxx$1_required=true],
  65. [$3], [optional], [ax_cxx_compile_cxx$1_required=false],
  66. [m4_fatal([invalid third argument `$3' to AX_CXX_COMPILE_STDCXX])])
  67. AC_LANG_PUSH([C++])dnl
  68. CXX_DIALECT=""
  69. ac_success=no
  70. AC_CACHE_CHECK(whether $CXX supports C++$1 features by default,
  71. ax_cv_cxx_compile_cxx$1,
  72. [AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],
  73. [ax_cv_cxx_compile_cxx$1=yes],
  74. [ax_cv_cxx_compile_cxx$1=no])])
  75. if test x$ax_cv_cxx_compile_cxx$1 = xyes; then
  76. ac_success=yes
  77. fi
  78. m4_if([$2], [noext], [], [dnl
  79. if test x$ac_success = xno; then
  80. for alternative in ${ax_cxx_compile_alternatives}; do
  81. switch="-std=gnu++${alternative}"
  82. cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_$switch])
  83. AC_CACHE_CHECK(whether $CXX supports C++$1 features with $switch,
  84. $cachevar,
  85. [ac_save_CXX="$CXX"
  86. CXX="$CXX $switch"
  87. AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],
  88. [eval $cachevar=yes],
  89. [eval $cachevar=no])
  90. CXX="$ac_save_CXX"])
  91. if eval test x\$$cachevar = xyes; then
  92. CXX_DIALECT="$switch"
  93. CXX="$CXX $switch"
  94. if test -n "$CXXCPP" ; then
  95. CXXCPP="$CXXCPP $switch"
  96. fi
  97. ac_success=yes
  98. break
  99. fi
  100. done
  101. fi])
  102. m4_if([$2], [ext], [], [dnl
  103. if test x$ac_success = xno; then
  104. dnl HP's aCC needs +std=c++11 according to:
  105. dnl http://h21007.www2.hp.com/portal/download/files/unprot/aCxx/PDF_Release_Notes/769149-001.pdf
  106. dnl Cray's crayCC needs "-h std=c++11"
  107. for alternative in ${ax_cxx_compile_alternatives}; do
  108. for switch in -std=c++${alternative} +std=c++${alternative} "-h std=c++${alternative}"; do
  109. cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_$switch])
  110. AC_CACHE_CHECK(whether $CXX supports C++$1 features with $switch,
  111. $cachevar,
  112. [ac_save_CXX="$CXX"
  113. CXX="$CXX $switch"
  114. AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],
  115. [eval $cachevar=yes],
  116. [eval $cachevar=no])
  117. CXX="$ac_save_CXX"])
  118. if eval test x\$$cachevar = xyes; then
  119. CXX_DIALECT="$switch"
  120. CXX="$CXX $switch"
  121. if test -n "$CXXCPP" ; then
  122. CXXCPP="$CXXCPP $switch"
  123. fi
  124. ac_success=yes
  125. break
  126. fi
  127. done
  128. if test x$ac_success = xyes; then
  129. break
  130. fi
  131. done
  132. fi])
  133. AC_LANG_POP([C++])
  134. if test x$ax_cxx_compile_cxx$1_required = xtrue; then
  135. if test x$ac_success = xno; then
  136. AC_MSG_ERROR([*** A compiler with support for C++$1 language features is required.])
  137. fi
  138. fi
  139. if test x$ac_success = xno; then
  140. HAVE_CXX$1=0
  141. AC_MSG_NOTICE([No compiler with C++$1 support was found])
  142. else
  143. HAVE_CXX$1=1
  144. AC_DEFINE(HAVE_CXX$1,1,
  145. [define if the compiler supports basic C++$1 syntax])
  146. fi
  147. AC_SUBST(HAVE_CXX$1)
  148. AC_SUBST(CXX_DIALECT)
  149. ])
  150. dnl Test body for checking C++11 support
  151. m4_define([_AX_CXX_COMPILE_STDCXX_testbody_11],
  152. _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
  153. )
  154. dnl Test body for checking C++14 support
  155. m4_define([_AX_CXX_COMPILE_STDCXX_testbody_14],
  156. _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
  157. _AX_CXX_COMPILE_STDCXX_testbody_new_in_14
  158. )
  159. m4_define([_AX_CXX_COMPILE_STDCXX_testbody_17],
  160. _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
  161. _AX_CXX_COMPILE_STDCXX_testbody_new_in_14
  162. _AX_CXX_COMPILE_STDCXX_testbody_new_in_17
  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. #elif __cplusplus < 201103L
  171. #error "This is not a C++11 compiler"
  172. #else
  173. namespace cxx11
  174. {
  175. namespace test_static_assert
  176. {
  177. template <typename T>
  178. struct check
  179. {
  180. static_assert(sizeof(int) <= sizeof(T), "not big enough");
  181. };
  182. }
  183. namespace test_final_override
  184. {
  185. struct Base
  186. {
  187. virtual void f() {}
  188. };
  189. struct Derived : public Base
  190. {
  191. virtual void f() override {}
  192. };
  193. }
  194. namespace test_double_right_angle_brackets
  195. {
  196. template < typename T >
  197. struct check {};
  198. typedef check<void> single_type;
  199. typedef check<check<void>> double_type;
  200. typedef check<check<check<void>>> triple_type;
  201. typedef check<check<check<check<void>>>> quadruple_type;
  202. }
  203. namespace test_decltype
  204. {
  205. int
  206. f()
  207. {
  208. int a = 1;
  209. decltype(a) b = 2;
  210. return a + b;
  211. }
  212. }
  213. namespace test_type_deduction
  214. {
  215. template < typename T1, typename T2 >
  216. struct is_same
  217. {
  218. static const bool value = false;
  219. };
  220. template < typename T >
  221. struct is_same<T, T>
  222. {
  223. static const bool value = true;
  224. };
  225. template < typename T1, typename T2 >
  226. auto
  227. add(T1 a1, T2 a2) -> decltype(a1 + a2)
  228. {
  229. return a1 + a2;
  230. }
  231. int
  232. test(const int c, volatile int v)
  233. {
  234. static_assert(is_same<int, decltype(0)>::value == true, "");
  235. static_assert(is_same<int, decltype(c)>::value == false, "");
  236. static_assert(is_same<int, decltype(v)>::value == false, "");
  237. auto ac = c;
  238. auto av = v;
  239. auto sumi = ac + av + 'x';
  240. auto sumf = ac + av + 1.0;
  241. static_assert(is_same<int, decltype(ac)>::value == true, "");
  242. static_assert(is_same<int, decltype(av)>::value == true, "");
  243. static_assert(is_same<int, decltype(sumi)>::value == true, "");
  244. static_assert(is_same<int, decltype(sumf)>::value == false, "");
  245. static_assert(is_same<int, decltype(add(c, v))>::value == true, "");
  246. return (sumf > 0.0) ? sumi : add(c, v);
  247. }
  248. }
  249. namespace test_noexcept
  250. {
  251. int f() { return 0; }
  252. int g() noexcept { return 0; }
  253. static_assert(noexcept(f()) == false, "");
  254. static_assert(noexcept(g()) == true, "");
  255. }
  256. namespace test_constexpr
  257. {
  258. template < typename CharT >
  259. unsigned long constexpr
  260. strlen_c_r(const CharT *const s, const unsigned long acc) noexcept
  261. {
  262. return *s ? strlen_c_r(s + 1, acc + 1) : acc;
  263. }
  264. template < typename CharT >
  265. unsigned long constexpr
  266. strlen_c(const CharT *const s) noexcept
  267. {
  268. return strlen_c_r(s, 0UL);
  269. }
  270. static_assert(strlen_c("") == 0UL, "");
  271. static_assert(strlen_c("1") == 1UL, "");
  272. static_assert(strlen_c("example") == 7UL, "");
  273. static_assert(strlen_c("another\0example") == 7UL, "");
  274. }
  275. namespace test_rvalue_references
  276. {
  277. template < int N >
  278. struct answer
  279. {
  280. static constexpr int value = N;
  281. };
  282. answer<1> f(int&) { return answer<1>(); }
  283. answer<2> f(const int&) { return answer<2>(); }
  284. answer<3> f(int&&) { return answer<3>(); }
  285. void
  286. test()
  287. {
  288. int i = 0;
  289. const int c = 0;
  290. static_assert(decltype(f(i))::value == 1, "");
  291. static_assert(decltype(f(c))::value == 2, "");
  292. static_assert(decltype(f(0))::value == 3, "");
  293. }
  294. }
  295. namespace test_uniform_initialization
  296. {
  297. struct test
  298. {
  299. static const int zero {};
  300. static const int one {1};
  301. };
  302. static_assert(test::zero == 0, "");
  303. static_assert(test::one == 1, "");
  304. }
  305. namespace test_lambdas
  306. {
  307. void
  308. test1()
  309. {
  310. auto lambda1 = [](){};
  311. auto lambda2 = lambda1;
  312. lambda1();
  313. lambda2();
  314. }
  315. int
  316. test2()
  317. {
  318. auto a = [](int i, int j){ return i + j; }(1, 2);
  319. auto b = []() -> int { return '0'; }();
  320. auto c = [=](){ return a + b; }();
  321. auto d = [&](){ return c; }();
  322. auto e = [a, &b](int x) mutable {
  323. const auto identity = [](int y){ return y; };
  324. for (auto i = 0; i < a; ++i)
  325. a += b--;
  326. return x + identity(a + b);
  327. }(0);
  328. return a + b + c + d + e;
  329. }
  330. int
  331. test3()
  332. {
  333. const auto nullary = [](){ return 0; };
  334. const auto unary = [](int x){ return x; };
  335. using nullary_t = decltype(nullary);
  336. using unary_t = decltype(unary);
  337. const auto higher1st = [](nullary_t f){ return f(); };
  338. const auto higher2nd = [unary](nullary_t f1){
  339. return [unary, f1](unary_t f2){ return f2(unary(f1())); };
  340. };
  341. return higher1st(nullary) + higher2nd(nullary)(unary);
  342. }
  343. }
  344. namespace test_variadic_templates
  345. {
  346. template <int...>
  347. struct sum;
  348. template <int N0, int... N1toN>
  349. struct sum<N0, N1toN...>
  350. {
  351. static constexpr auto value = N0 + sum<N1toN...>::value;
  352. };
  353. template <>
  354. struct sum<>
  355. {
  356. static constexpr auto value = 0;
  357. };
  358. static_assert(sum<>::value == 0, "");
  359. static_assert(sum<1>::value == 1, "");
  360. static_assert(sum<23>::value == 23, "");
  361. static_assert(sum<1, 2>::value == 3, "");
  362. static_assert(sum<5, 5, 11>::value == 21, "");
  363. static_assert(sum<2, 3, 5, 7, 11, 13>::value == 41, "");
  364. }
  365. // http://stackoverflow.com/questions/13728184/template-aliases-and-sfinae
  366. // Clang 3.1 fails with headers of libstd++ 4.8.3 when using std::function
  367. // because of this.
  368. namespace test_template_alias_sfinae
  369. {
  370. struct foo {};
  371. template<typename T>
  372. using member = typename T::member_type;
  373. template<typename T>
  374. void func(...) {}
  375. template<typename T>
  376. void func(member<T>*) {}
  377. void test();
  378. void test() { func<foo>(0); }
  379. }
  380. } // namespace cxx11
  381. #endif // __cplusplus >= 201103L
  382. ]])
  383. dnl Tests for new features in C++14
  384. m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_14], [[
  385. // If the compiler admits that it is not ready for C++14, why torture it?
  386. // Hopefully, this will speed up the test.
  387. #ifndef __cplusplus
  388. #error "This is not a C++ compiler"
  389. #elif __cplusplus < 201402L
  390. #error "This is not a C++14 compiler"
  391. #else
  392. namespace cxx14
  393. {
  394. namespace test_polymorphic_lambdas
  395. {
  396. int
  397. test()
  398. {
  399. const auto lambda = [](auto&&... args){
  400. const auto istiny = [](auto x){
  401. return (sizeof(x) == 1UL) ? 1 : 0;
  402. };
  403. const int aretiny[] = { istiny(args)... };
  404. return aretiny[0];
  405. };
  406. return lambda(1, 1L, 1.0f, '1');
  407. }
  408. }
  409. namespace test_binary_literals
  410. {
  411. constexpr auto ivii = 0b0000000000101010;
  412. static_assert(ivii == 42, "wrong value");
  413. }
  414. namespace test_generalized_constexpr
  415. {
  416. template < typename CharT >
  417. constexpr unsigned long
  418. strlen_c(const CharT *const s) noexcept
  419. {
  420. auto length = 0UL;
  421. for (auto p = s; *p; ++p)
  422. ++length;
  423. return length;
  424. }
  425. static_assert(strlen_c("") == 0UL, "");
  426. static_assert(strlen_c("x") == 1UL, "");
  427. static_assert(strlen_c("test") == 4UL, "");
  428. static_assert(strlen_c("another\0test") == 7UL, "");
  429. }
  430. namespace test_lambda_init_capture
  431. {
  432. int
  433. test()
  434. {
  435. auto x = 0;
  436. const auto lambda1 = [a = x](int b){ return a + b; };
  437. const auto lambda2 = [a = lambda1(x)](){ return a; };
  438. return lambda2();
  439. }
  440. }
  441. namespace test_digit_separators
  442. {
  443. constexpr auto ten_million = 100'000'000;
  444. static_assert(ten_million == 100000000, "");
  445. }
  446. namespace test_return_type_deduction
  447. {
  448. auto f(int& x) { return x; }
  449. decltype(auto) g(int& x) { return x; }
  450. template < typename T1, typename T2 >
  451. struct is_same
  452. {
  453. static constexpr auto value = false;
  454. };
  455. template < typename T >
  456. struct is_same<T, T>
  457. {
  458. static constexpr auto value = true;
  459. };
  460. int
  461. test()
  462. {
  463. auto x = 0;
  464. static_assert(is_same<int, decltype(f(x))>::value, "");
  465. static_assert(is_same<int&, decltype(g(x))>::value, "");
  466. return x;
  467. }
  468. }
  469. } // namespace cxx14
  470. #endif // __cplusplus >= 201402L
  471. ]])
  472. dnl Tests for new features in C++17
  473. m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_17], [[
  474. // If the compiler admits that it is not ready for C++17, why torture it?
  475. // Hopefully, this will speed up the test.
  476. #ifndef __cplusplus
  477. #error "This is not a C++ compiler"
  478. #elif __cplusplus <= 201402L
  479. #error "This is not a C++17 compiler"
  480. #else
  481. #if defined(__clang__)
  482. #define REALLY_CLANG
  483. #else
  484. #if defined(__GNUC__)
  485. #define REALLY_GCC
  486. #endif
  487. #endif
  488. #include <initializer_list>
  489. #include <utility>
  490. #include <type_traits>
  491. namespace cxx17
  492. {
  493. #if !defined(REALLY_CLANG)
  494. namespace test_constexpr_lambdas
  495. {
  496. // TODO: test it with clang++ from git
  497. constexpr int foo = [](){return 42;}();
  498. }
  499. #endif // !defined(REALLY_CLANG)
  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. #if !defined(REALLY_CLANG)
  679. namespace test_template_argument_deduction_for_class_templates
  680. {
  681. // TODO: test it with clang++ from git
  682. template <typename T1, typename T2>
  683. struct pair
  684. {
  685. pair (T1 p1, T2 p2)
  686. : m1 {p1},
  687. m2 {p2}
  688. {}
  689. T1 m1;
  690. T2 m2;
  691. };
  692. void f()
  693. {
  694. [[maybe_unused]] auto p = pair{13, 42u};
  695. }
  696. }
  697. #endif // !defined(REALLY_CLANG)
  698. namespace test_non_type_auto_template_parameters
  699. {
  700. template <auto n>
  701. struct B
  702. {};
  703. B<5> b1;
  704. B<'a'> b2;
  705. }
  706. #if !defined(REALLY_CLANG)
  707. namespace test_structured_bindings
  708. {
  709. // TODO: test it with clang++ from git
  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. #endif // !defined(REALLY_CLANG)
  736. #if !defined(REALLY_CLANG)
  737. namespace test_exception_spec_type_system
  738. {
  739. // TODO: test it with clang++ from git
  740. struct Good {};
  741. struct Bad {};
  742. void g1() noexcept;
  743. void g2();
  744. template<typename T>
  745. Bad
  746. f(T*, T*);
  747. template<typename T1, typename T2>
  748. Good
  749. f(T1*, T2*);
  750. static_assert (std::is_same_v<Good, decltype(f(g1, g2))>);
  751. }
  752. #endif // !defined(REALLY_CLANG)
  753. namespace test_inline_variables
  754. {
  755. template<class T> void f(T)
  756. {}
  757. template<class T> inline T g(T)
  758. {
  759. return T{};
  760. }
  761. template<> inline void f<>(int)
  762. {}
  763. template<> int g<>(int)
  764. {
  765. return 5;
  766. }
  767. }
  768. } // namespace cxx17
  769. #endif // __cplusplus <= 201402L
  770. ]])