You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ax_cxx_compile_stdcxx.m4 14KB

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