ax_cxx_compile_stdcxx.m4 20 KB

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