[test] Add getblockchaininfo functional test
[bitcoinplatinum.git] / build-aux / m4 / ax_cxx_compile_stdcxx.m4
blobf147cee3b117f80b22544976385e267b26f46dd9
1 # ===========================================================================
2 #   http://www.gnu.org/software/autoconf-archive/ax_cxx_compile_stdcxx.html
3 # ===========================================================================
5 # SYNOPSIS
7 #   AX_CXX_COMPILE_STDCXX(VERSION, [ext|noext], [mandatory|optional])
9 # DESCRIPTION
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).
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.
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.
28 # LICENSE
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>
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.
42 #serial 4
44 dnl  This macro is based on the code from the AX_CXX_COMPILE_STDCXX_11 macro
45 dnl  (serial version number 13).
47 AC_DEFUN([AX_CXX_COMPILE_STDCXX], [dnl
48   m4_if([$1], [11], [],
49         [$1], [14], [],
50         [$1], [17], [m4_fatal([support for C++17 not yet implemented in AX_CXX_COMPILE_STDCXX])],
51         [m4_fatal([invalid first argument `$1' to AX_CXX_COMPILE_STDCXX])])dnl
52   m4_if([$2], [], [],
53         [$2], [ext], [],
54         [$2], [noext], [],
55         [m4_fatal([invalid second argument `$2' to AX_CXX_COMPILE_STDCXX])])dnl
56   m4_if([$3], [], [ax_cxx_compile_cxx$1_required=true],
57         [$3], [mandatory], [ax_cxx_compile_cxx$1_required=true],
58         [$3], [optional], [ax_cxx_compile_cxx$1_required=false],
59         [m4_fatal([invalid third argument `$3' to AX_CXX_COMPILE_STDCXX])])
60   m4_if([$4], [], [ax_cxx_compile_cxx$1_try_default=true],
61         [$4], [default], [ax_cxx_compile_cxx$1_try_default=true],
62         [$4], [nodefault], [ax_cxx_compile_cxx$1_try_default=false],
63         [m4_fatal([invalid fourth argument `$4' to AX_CXX_COMPILE_STDCXX])])
64   AC_LANG_PUSH([C++])dnl
65   ac_success=no
67   m4_if([$4], [nodefault], [], [dnl
68   AC_CACHE_CHECK(whether $CXX supports C++$1 features by default,
69   ax_cv_cxx_compile_cxx$1,
70   [AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],
71     [ax_cv_cxx_compile_cxx$1=yes],
72     [ax_cv_cxx_compile_cxx$1=no])])
73   if test x$ax_cv_cxx_compile_cxx$1 = xyes; then
74     ac_success=yes
75   fi])
77   m4_if([$2], [noext], [], [dnl
78   if test x$ac_success = xno; then
79     for switch in -std=gnu++$1 -std=gnu++0x; do
80       cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_$switch])
81       AC_CACHE_CHECK(whether $CXX supports C++$1 features with $switch,
82                      $cachevar,
83         [ac_save_CXX="$CXX"
84          CXX="$CXX $switch"
85          AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],
86           [eval $cachevar=yes],
87           [eval $cachevar=no])
88          CXX="$ac_save_CXX"])
89       if eval test x\$$cachevar = xyes; then
90         CXX="$CXX $switch"
91         if test -n "$CXXCPP" ; then
92           CXXCPP="$CXXCPP $switch"
93         fi
94         ac_success=yes
95         break
96       fi
97     done
98   fi])
100   m4_if([$2], [ext], [], [dnl
101   if test x$ac_success = xno; then
102     dnl HP's aCC needs +std=c++11 according to:
103     dnl http://h21007.www2.hp.com/portal/download/files/unprot/aCxx/PDF_Release_Notes/769149-001.pdf
104     dnl Cray's crayCC needs "-h std=c++11"
105     for switch in -std=c++$1 -std=c++0x +std=c++$1 "-h std=c++$1"; do
106       cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_$switch])
107       AC_CACHE_CHECK(whether $CXX supports C++$1 features with $switch,
108                      $cachevar,
109         [ac_save_CXX="$CXX"
110          CXX="$CXX $switch"
111          AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],
112           [eval $cachevar=yes],
113           [eval $cachevar=no])
114          CXX="$ac_save_CXX"])
115       if eval test x\$$cachevar = xyes; then
116         CXX="$CXX $switch"
117         if test -n "$CXXCPP" ; then
118           CXXCPP="$CXXCPP $switch"
119         fi
120         ac_success=yes
121         break
122       fi
123     done
124   fi])
125   AC_LANG_POP([C++])
126   if test x$ax_cxx_compile_cxx$1_required = xtrue; then
127     if test x$ac_success = xno; then
128       AC_MSG_ERROR([*** A compiler with support for C++$1 language features is required.])
129     fi
130   fi
131   if test x$ac_success = xno; then
132     HAVE_CXX$1=0
133     AC_MSG_NOTICE([No compiler with C++$1 support was found])
134   else
135     HAVE_CXX$1=1
136     AC_DEFINE(HAVE_CXX$1,1,
137               [define if the compiler supports basic C++$1 syntax])
138   fi
139   AC_SUBST(HAVE_CXX$1)
143 dnl  Test body for checking C++11 support
145 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_11],
146   _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
150 dnl  Test body for checking C++14 support
152 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_14],
153   _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
154   _AX_CXX_COMPILE_STDCXX_testbody_new_in_14
158 dnl  Tests for new features in C++11
160 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_11], [[
162 // If the compiler admits that it is not ready for C++11, why torture it?
163 // Hopefully, this will speed up the test.
165 #ifndef __cplusplus
167 #error "This is not a C++ compiler"
169 #elif __cplusplus < 201103L
171 #error "This is not a C++11 compiler"
173 #else
175 namespace cxx11
178   namespace test_static_assert
179   {
181     template <typename T>
182     struct check
183     {
184       static_assert(sizeof(int) <= sizeof(T), "not big enough");
185     };
187   }
189   namespace test_final_override
190   {
192     struct Base
193     {
194       virtual void f() {}
195     };
197     struct Derived : public Base
198     {
199       virtual void f() override {}
200     };
202   }
204   namespace test_double_right_angle_brackets
205   {
207     template < typename T >
208     struct check {};
210     typedef check<void> single_type;
211     typedef check<check<void>> double_type;
212     typedef check<check<check<void>>> triple_type;
213     typedef check<check<check<check<void>>>> quadruple_type;
215   }
217   namespace test_decltype
218   {
220     int
221     f()
222     {
223       int a = 1;
224       decltype(a) b = 2;
225       return a + b;
226     }
228   }
230   namespace test_type_deduction
231   {
233     template < typename T1, typename T2 >
234     struct is_same
235     {
236       static const bool value = false;
237     };
239     template < typename T >
240     struct is_same<T, T>
241     {
242       static const bool value = true;
243     };
245     template < typename T1, typename T2 >
246     auto
247     add(T1 a1, T2 a2) -> decltype(a1 + a2)
248     {
249       return a1 + a2;
250     }
252     int
253     test(const int c, volatile int v)
254     {
255       static_assert(is_same<int, decltype(0)>::value == true, "");
256       static_assert(is_same<int, decltype(c)>::value == false, "");
257       static_assert(is_same<int, decltype(v)>::value == false, "");
258       auto ac = c;
259       auto av = v;
260       auto sumi = ac + av + 'x';
261       auto sumf = ac + av + 1.0;
262       static_assert(is_same<int, decltype(ac)>::value == true, "");
263       static_assert(is_same<int, decltype(av)>::value == true, "");
264       static_assert(is_same<int, decltype(sumi)>::value == true, "");
265       static_assert(is_same<int, decltype(sumf)>::value == false, "");
266       static_assert(is_same<int, decltype(add(c, v))>::value == true, "");
267       return (sumf > 0.0) ? sumi : add(c, v);
268     }
270   }
272   namespace test_noexcept
273   {
275     int f() { return 0; }
276     int g() noexcept { return 0; }
278     static_assert(noexcept(f()) == false, "");
279     static_assert(noexcept(g()) == true, "");
281   }
283   namespace test_constexpr
284   {
286     template < typename CharT >
287     unsigned long constexpr
288     strlen_c_r(const CharT *const s, const unsigned long acc) noexcept
289     {
290       return *s ? strlen_c_r(s + 1, acc + 1) : acc;
291     }
293     template < typename CharT >
294     unsigned long constexpr
295     strlen_c(const CharT *const s) noexcept
296     {
297       return strlen_c_r(s, 0UL);
298     }
300     static_assert(strlen_c("") == 0UL, "");
301     static_assert(strlen_c("1") == 1UL, "");
302     static_assert(strlen_c("example") == 7UL, "");
303     static_assert(strlen_c("another\0example") == 7UL, "");
305   }
307   namespace test_rvalue_references
308   {
310     template < int N >
311     struct answer
312     {
313       static constexpr int value = N;
314     };
316     answer<1> f(int&)       { return answer<1>(); }
317     answer<2> f(const int&) { return answer<2>(); }
318     answer<3> f(int&&)      { return answer<3>(); }
320     void
321     test()
322     {
323       int i = 0;
324       const int c = 0;
325       static_assert(decltype(f(i))::value == 1, "");
326       static_assert(decltype(f(c))::value == 2, "");
327       static_assert(decltype(f(0))::value == 3, "");
328     }
330   }
332   namespace test_uniform_initialization
333   {
335     struct test
336     {
337       static const int zero {};
338       static const int one {1};
339     };
341     static_assert(test::zero == 0, "");
342     static_assert(test::one == 1, "");
344   }
346   namespace test_lambdas
347   {
349     void
350     test1()
351     {
352       auto lambda1 = [](){};
353       auto lambda2 = lambda1;
354       lambda1();
355       lambda2();
356     }
358     int
359     test2()
360     {
361       auto a = [](int i, int j){ return i + j; }(1, 2);
362       auto b = []() -> int { return '0'; }();
363       auto c = [=](){ return a + b; }();
364       auto d = [&](){ return c; }();
365       auto e = [a, &b](int x) mutable {
366         const auto identity = [](int y){ return y; };
367         for (auto i = 0; i < a; ++i)
368           a += b--;
369         return x + identity(a + b);
370       }(0);
371       return a + b + c + d + e;
372     }
374     int
375     test3()
376     {
377       const auto nullary = [](){ return 0; };
378       const auto unary = [](int x){ return x; };
379       using nullary_t = decltype(nullary);
380       using unary_t = decltype(unary);
381       const auto higher1st = [](nullary_t f){ return f(); };
382       const auto higher2nd = [unary](nullary_t f1){
383         return [unary, f1](unary_t f2){ return f2(unary(f1())); };
384       };
385       return higher1st(nullary) + higher2nd(nullary)(unary);
386     }
388   }
390   namespace test_variadic_templates
391   {
393     template <int...>
394     struct sum;
396     template <int N0, int... N1toN>
397     struct sum<N0, N1toN...>
398     {
399       static constexpr auto value = N0 + sum<N1toN...>::value;
400     };
402     template <>
403     struct sum<>
404     {
405       static constexpr auto value = 0;
406     };
408     static_assert(sum<>::value == 0, "");
409     static_assert(sum<1>::value == 1, "");
410     static_assert(sum<23>::value == 23, "");
411     static_assert(sum<1, 2>::value == 3, "");
412     static_assert(sum<5, 5, 11>::value == 21, "");
413     static_assert(sum<2, 3, 5, 7, 11, 13>::value == 41, "");
415   }
417   // http://stackoverflow.com/questions/13728184/template-aliases-and-sfinae
418   // Clang 3.1 fails with headers of libstd++ 4.8.3 when using std::function
419   // because of this.
420   namespace test_template_alias_sfinae
421   {
423     struct foo {};
425     template<typename T>
426     using member = typename T::member_type;
428     template<typename T>
429     void func(...) {}
431     template<typename T>
432     void func(member<T>*) {}
434     void test();
436     void test() { func<foo>(0); }
438   }
440 }  // namespace cxx11
442 #endif  // __cplusplus >= 201103L
447 dnl  Tests for new features in C++14
449 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_14], [[
451 // If the compiler admits that it is not ready for C++14, why torture it?
452 // Hopefully, this will speed up the test.
454 #ifndef __cplusplus
456 #error "This is not a C++ compiler"
458 #elif __cplusplus < 201402L
460 #error "This is not a C++14 compiler"
462 #else
464 namespace cxx14
467   namespace test_polymorphic_lambdas
468   {
470     int
471     test()
472     {
473       const auto lambda = [](auto&&... args){
474         const auto istiny = [](auto x){
475           return (sizeof(x) == 1UL) ? 1 : 0;
476         };
477         const int aretiny[] = { istiny(args)... };
478         return aretiny[0];
479       };
480       return lambda(1, 1L, 1.0f, '1');
481     }
483   }
485   namespace test_binary_literals
486   {
488     constexpr auto ivii = 0b0000000000101010;
489     static_assert(ivii == 42, "wrong value");
491   }
493   namespace test_generalized_constexpr
494   {
496     template < typename CharT >
497     constexpr unsigned long
498     strlen_c(const CharT *const s) noexcept
499     {
500       auto length = 0UL;
501       for (auto p = s; *p; ++p)
502         ++length;
503       return length;
504     }
506     static_assert(strlen_c("") == 0UL, "");
507     static_assert(strlen_c("x") == 1UL, "");
508     static_assert(strlen_c("test") == 4UL, "");
509     static_assert(strlen_c("another\0test") == 7UL, "");
511   }
513   namespace test_lambda_init_capture
514   {
516     int
517     test()
518     {
519       auto x = 0;
520       const auto lambda1 = [a = x](int b){ return a + b; };
521       const auto lambda2 = [a = lambda1(x)](){ return a; };
522       return lambda2();
523     }
525   }
527   namespace test_digit_seperators
528   {
530     constexpr auto ten_million = 100'000'000;
531     static_assert(ten_million == 100000000, "");
533   }
535   namespace test_return_type_deduction
536   {
538     auto f(int& x) { return x; }
539     decltype(auto) g(int& x) { return x; }
541     template < typename T1, typename T2 >
542     struct is_same
543     {
544       static constexpr auto value = false;
545     };
547     template < typename T >
548     struct is_same<T, T>
549     {
550       static constexpr auto value = true;
551     };
553     int
554     test()
555     {
556       auto x = 0;
557       static_assert(is_same<int, decltype(f(x))>::value, "");
558       static_assert(is_same<int&, decltype(g(x))>::value, "");
559       return x;
560     }
562   }
564 }  // namespace cxx14
566 #endif  // __cplusplus >= 201402L