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