ax_cxx_compile_stdcxx.m4 13 KB

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