2
0

ax_cxx_compile_stdcxx.m4 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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. #
  37. # Copying and distribution of this file, with or without modification, are
  38. # permitted in any medium without royalty provided the copyright notice
  39. # and this notice are preserved. This file is offered as-is, without any
  40. # warranty.
  41. #serial 4
  42. dnl This macro is based on the code from the AX_CXX_COMPILE_STDCXX_11 macro
  43. dnl (serial version number 13).
  44. AC_DEFUN([AX_CXX_COMPILE_STDCXX], [dnl
  45. m4_if([$1], [11], [],
  46. [$1], [14], [],
  47. [$1], [17], [m4_fatal([support for C++17 not yet implemented in AX_CXX_COMPILE_STDCXX])],
  48. [m4_fatal([invalid first argument `$1' to AX_CXX_COMPILE_STDCXX])])dnl
  49. m4_if([$2], [], [],
  50. [$2], [ext], [],
  51. [$2], [noext], [],
  52. [m4_fatal([invalid second argument `$2' to AX_CXX_COMPILE_STDCXX])])dnl
  53. m4_if([$3], [], [ax_cxx_compile_cxx$1_required=true],
  54. [$3], [mandatory], [ax_cxx_compile_cxx$1_required=true],
  55. [$3], [optional], [ax_cxx_compile_cxx$1_required=false],
  56. [m4_fatal([invalid third argument `$3' to AX_CXX_COMPILE_STDCXX])])
  57. AC_LANG_PUSH([C++])dnl
  58. ac_success=no
  59. AC_CACHE_CHECK(whether $CXX supports C++$1 features by default,
  60. ax_cv_cxx_compile_cxx$1,
  61. [AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],
  62. [ax_cv_cxx_compile_cxx$1=yes],
  63. [ax_cv_cxx_compile_cxx$1=no])])
  64. if test x$ax_cv_cxx_compile_cxx$1 = xyes; then
  65. ac_success=yes
  66. fi
  67. m4_if([$2], [noext], [], [dnl
  68. if test x$ac_success = xno; then
  69. for switch in -std=gnu++$1 -std=gnu++0x; do
  70. cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_$switch])
  71. AC_CACHE_CHECK(whether $CXX supports C++$1 features with $switch,
  72. $cachevar,
  73. [ac_save_CXX="$CXX"
  74. CXX="$CXX $switch"
  75. AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],
  76. [eval $cachevar=yes],
  77. [eval $cachevar=no])
  78. CXX="$ac_save_CXX"])
  79. if eval test x\$$cachevar = xyes; then
  80. CXX="$CXX $switch"
  81. if test -n "$CXXCPP" ; then
  82. CXXCPP="$CXXCPP $switch"
  83. fi
  84. ac_success=yes
  85. break
  86. fi
  87. done
  88. fi])
  89. m4_if([$2], [ext], [], [dnl
  90. if test x$ac_success = xno; then
  91. dnl HP's aCC needs +std=c++11 according to:
  92. dnl http://h21007.www2.hp.com/portal/download/files/unprot/aCxx/PDF_Release_Notes/769149-001.pdf
  93. dnl Cray's crayCC needs "-h std=c++11"
  94. for switch in -std=c++$1 -std=c++0x +std=c++$1 "-h std=c++$1"; do
  95. cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_$switch])
  96. AC_CACHE_CHECK(whether $CXX supports C++$1 features with $switch,
  97. $cachevar,
  98. [ac_save_CXX="$CXX"
  99. CXX="$CXX $switch"
  100. AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],
  101. [eval $cachevar=yes],
  102. [eval $cachevar=no])
  103. CXX="$ac_save_CXX"])
  104. if eval test x\$$cachevar = xyes; then
  105. CXX="$CXX $switch"
  106. if test -n "$CXXCPP" ; then
  107. CXXCPP="$CXXCPP $switch"
  108. fi
  109. ac_success=yes
  110. break
  111. fi
  112. done
  113. fi])
  114. AC_LANG_POP([C++])
  115. if test x$ax_cxx_compile_cxx$1_required = xtrue; then
  116. if test x$ac_success = xno; then
  117. AC_MSG_ERROR([*** A compiler with support for C++$1 language features is required.])
  118. fi
  119. fi
  120. if test x$ac_success = xno; then
  121. HAVE_CXX$1=0
  122. AC_MSG_NOTICE([No compiler with C++$1 support was found])
  123. else
  124. HAVE_CXX$1=1
  125. AC_DEFINE(HAVE_CXX$1,1,
  126. [define if the compiler supports basic C++$1 syntax])
  127. fi
  128. AC_SUBST(HAVE_CXX$1)
  129. ])
  130. dnl Test body for checking C++11 support
  131. m4_define([_AX_CXX_COMPILE_STDCXX_testbody_11],
  132. _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
  133. )
  134. dnl Test body for checking C++14 support
  135. m4_define([_AX_CXX_COMPILE_STDCXX_testbody_14],
  136. _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
  137. _AX_CXX_COMPILE_STDCXX_testbody_new_in_14
  138. )
  139. dnl Tests for new features in C++11
  140. m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_11], [[
  141. // If the compiler admits that it is not ready for C++11, why torture it?
  142. // Hopefully, this will speed up the test.
  143. #ifndef __cplusplus
  144. #error "This is not a C++ compiler"
  145. #elif __cplusplus < 201103L
  146. #error "This is not a C++11 compiler"
  147. #else
  148. namespace cxx11
  149. {
  150. namespace test_static_assert
  151. {
  152. template <typename T>
  153. struct check
  154. {
  155. static_assert(sizeof(int) <= sizeof(T), "not big enough");
  156. };
  157. }
  158. namespace test_final_override
  159. {
  160. struct Base
  161. {
  162. virtual void f() {}
  163. };
  164. struct Derived : public Base
  165. {
  166. virtual void f() override {}
  167. };
  168. }
  169. namespace test_double_right_angle_brackets
  170. {
  171. template < typename T >
  172. struct check {};
  173. typedef check<void> single_type;
  174. typedef check<check<void>> double_type;
  175. typedef check<check<check<void>>> triple_type;
  176. typedef check<check<check<check<void>>>> quadruple_type;
  177. }
  178. namespace test_decltype
  179. {
  180. int
  181. f()
  182. {
  183. int a = 1;
  184. decltype(a) b = 2;
  185. return a + b;
  186. }
  187. }
  188. namespace test_type_deduction
  189. {
  190. template < typename T1, typename T2 >
  191. struct is_same
  192. {
  193. static const bool value = false;
  194. };
  195. template < typename T >
  196. struct is_same<T, T>
  197. {
  198. static const bool value = true;
  199. };
  200. template < typename T1, typename T2 >
  201. auto
  202. add(T1 a1, T2 a2) -> decltype(a1 + a2)
  203. {
  204. return a1 + a2;
  205. }
  206. int
  207. test(const int c, volatile int v)
  208. {
  209. static_assert(is_same<int, decltype(0)>::value == true, "");
  210. static_assert(is_same<int, decltype(c)>::value == false, "");
  211. static_assert(is_same<int, decltype(v)>::value == false, "");
  212. auto ac = c;
  213. auto av = v;
  214. auto sumi = ac + av + 'x';
  215. auto sumf = ac + av + 1.0;
  216. static_assert(is_same<int, decltype(ac)>::value == true, "");
  217. static_assert(is_same<int, decltype(av)>::value == true, "");
  218. static_assert(is_same<int, decltype(sumi)>::value == true, "");
  219. static_assert(is_same<int, decltype(sumf)>::value == false, "");
  220. static_assert(is_same<int, decltype(add(c, v))>::value == true, "");
  221. return (sumf > 0.0) ? sumi : add(c, v);
  222. }
  223. }
  224. namespace test_noexcept
  225. {
  226. int f() { return 0; }
  227. int g() noexcept { return 0; }
  228. static_assert(noexcept(f()) == false, "");
  229. static_assert(noexcept(g()) == true, "");
  230. }
  231. namespace test_constexpr
  232. {
  233. template < typename CharT >
  234. unsigned long constexpr
  235. strlen_c_r(const CharT *const s, const unsigned long acc) noexcept
  236. {
  237. return *s ? strlen_c_r(s + 1, acc + 1) : acc;
  238. }
  239. template < typename CharT >
  240. unsigned long constexpr
  241. strlen_c(const CharT *const s) noexcept
  242. {
  243. return strlen_c_r(s, 0UL);
  244. }
  245. static_assert(strlen_c("") == 0UL, "");
  246. static_assert(strlen_c("1") == 1UL, "");
  247. static_assert(strlen_c("example") == 7UL, "");
  248. static_assert(strlen_c("another\0example") == 7UL, "");
  249. }
  250. namespace test_rvalue_references
  251. {
  252. template < int N >
  253. struct answer
  254. {
  255. static constexpr int value = N;
  256. };
  257. answer<1> f(int&) { return answer<1>(); }
  258. answer<2> f(const int&) { return answer<2>(); }
  259. answer<3> f(int&&) { return answer<3>(); }
  260. void
  261. test()
  262. {
  263. int i = 0;
  264. const int c = 0;
  265. static_assert(decltype(f(i))::value == 1, "");
  266. static_assert(decltype(f(c))::value == 2, "");
  267. static_assert(decltype(f(0))::value == 3, "");
  268. }
  269. }
  270. namespace test_uniform_initialization
  271. {
  272. struct test
  273. {
  274. static const int zero {};
  275. static const int one {1};
  276. };
  277. static_assert(test::zero == 0, "");
  278. static_assert(test::one == 1, "");
  279. }
  280. namespace test_lambdas
  281. {
  282. void
  283. test1()
  284. {
  285. auto lambda1 = [](){};
  286. auto lambda2 = lambda1;
  287. lambda1();
  288. lambda2();
  289. }
  290. int
  291. test2()
  292. {
  293. auto a = [](int i, int j){ return i + j; }(1, 2);
  294. auto b = []() -> int { return '0'; }();
  295. auto c = [=](){ return a + b; }();
  296. auto d = [&](){ return c; }();
  297. auto e = [a, &b](int x) mutable {
  298. const auto identity = [](int y){ return y; };
  299. for (auto i = 0; i < a; ++i)
  300. a += b--;
  301. return x + identity(a + b);
  302. }(0);
  303. return a + b + c + d + e;
  304. }
  305. int
  306. test3()
  307. {
  308. const auto nullary = [](){ return 0; };
  309. const auto unary = [](int x){ return x; };
  310. using nullary_t = decltype(nullary);
  311. using unary_t = decltype(unary);
  312. const auto higher1st = [](nullary_t f){ return f(); };
  313. const auto higher2nd = [unary](nullary_t f1){
  314. return [unary, f1](unary_t f2){ return f2(unary(f1())); };
  315. };
  316. return higher1st(nullary) + higher2nd(nullary)(unary);
  317. }
  318. }
  319. namespace test_variadic_templates
  320. {
  321. template <int...>
  322. struct sum;
  323. template <int N0, int... N1toN>
  324. struct sum<N0, N1toN...>
  325. {
  326. static constexpr auto value = N0 + sum<N1toN...>::value;
  327. };
  328. template <>
  329. struct sum<>
  330. {
  331. static constexpr auto value = 0;
  332. };
  333. static_assert(sum<>::value == 0, "");
  334. static_assert(sum<1>::value == 1, "");
  335. static_assert(sum<23>::value == 23, "");
  336. static_assert(sum<1, 2>::value == 3, "");
  337. static_assert(sum<5, 5, 11>::value == 21, "");
  338. static_assert(sum<2, 3, 5, 7, 11, 13>::value == 41, "");
  339. }
  340. // http://stackoverflow.com/questions/13728184/template-aliases-and-sfinae
  341. // Clang 3.1 fails with headers of libstd++ 4.8.3 when using std::function
  342. // because of this.
  343. namespace test_template_alias_sfinae
  344. {
  345. struct foo {};
  346. template<typename T>
  347. using member = typename T::member_type;
  348. template<typename T>
  349. void func(...) {}
  350. template<typename T>
  351. void func(member<T>*) {}
  352. void test();
  353. void test() { func<foo>(0); }
  354. }
  355. } // namespace cxx11
  356. #endif // __cplusplus >= 201103L
  357. ]])
  358. dnl Tests for new features in C++14
  359. m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_14], [[
  360. // If the compiler admits that it is not ready for C++14, why torture it?
  361. // Hopefully, this will speed up the test.
  362. #ifndef __cplusplus
  363. #error "This is not a C++ compiler"
  364. #elif __cplusplus < 201402L
  365. #error "This is not a C++14 compiler"
  366. #else
  367. namespace cxx14
  368. {
  369. namespace test_polymorphic_lambdas
  370. {
  371. int
  372. test()
  373. {
  374. const auto lambda = [](auto&&... args){
  375. const auto istiny = [](auto x){
  376. return (sizeof(x) == 1UL) ? 1 : 0;
  377. };
  378. const int aretiny[] = { istiny(args)... };
  379. return aretiny[0];
  380. };
  381. return lambda(1, 1L, 1.0f, '1');
  382. }
  383. }
  384. namespace test_binary_literals
  385. {
  386. constexpr auto ivii = 0b0000000000101010;
  387. static_assert(ivii == 42, "wrong value");
  388. }
  389. namespace test_generalized_constexpr
  390. {
  391. template < typename CharT >
  392. constexpr unsigned long
  393. strlen_c(const CharT *const s) noexcept
  394. {
  395. auto length = 0UL;
  396. for (auto p = s; *p; ++p)
  397. ++length;
  398. return length;
  399. }
  400. static_assert(strlen_c("") == 0UL, "");
  401. static_assert(strlen_c("x") == 1UL, "");
  402. static_assert(strlen_c("test") == 4UL, "");
  403. static_assert(strlen_c("another\0test") == 7UL, "");
  404. }
  405. namespace test_lambda_init_capture
  406. {
  407. int
  408. test()
  409. {
  410. auto x = 0;
  411. const auto lambda1 = [a = x](int b){ return a + b; };
  412. const auto lambda2 = [a = lambda1(x)](){ return a; };
  413. return lambda2();
  414. }
  415. }
  416. namespace test_digit_seperators
  417. {
  418. constexpr auto ten_million = 100'000'000;
  419. static_assert(ten_million == 100000000, "");
  420. }
  421. namespace test_return_type_deduction
  422. {
  423. auto f(int& x) { return x; }
  424. decltype(auto) g(int& x) { return x; }
  425. template < typename T1, typename T2 >
  426. struct is_same
  427. {
  428. static constexpr auto value = false;
  429. };
  430. template < typename T >
  431. struct is_same<T, T>
  432. {
  433. static constexpr auto value = true;
  434. };
  435. int
  436. test()
  437. {
  438. auto x = 0;
  439. static_assert(is_same<int, decltype(f(x))>::value, "");
  440. static_assert(is_same<int&, decltype(g(x))>::value, "");
  441. return x;
  442. }
  443. }
  444. } // namespace cxx14
  445. #endif // __cplusplus >= 201402L
  446. ]])