ax_cxx_compile_stdcxx.m4 13 KB

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