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