Blame jemalloc/m4/ax_cxx_compile_stdcxx.m4

Packit 345191
# ===========================================================================
Packit 345191
#   http://www.gnu.org/software/autoconf-archive/ax_cxx_compile_stdcxx.html
Packit 345191
# ===========================================================================
Packit 345191
#
Packit 345191
# SYNOPSIS
Packit 345191
#
Packit 345191
#   AX_CXX_COMPILE_STDCXX(VERSION, [ext|noext], [mandatory|optional])
Packit 345191
#
Packit 345191
# DESCRIPTION
Packit 345191
#
Packit 345191
#   Check for baseline language coverage in the compiler for the specified
Packit 345191
#   version of the C++ standard.  If necessary, add switches to CXX and
Packit 345191
#   CXXCPP to enable support.  VERSION may be '11' (for the C++11 standard)
Packit 345191
#   or '14' (for the C++14 standard).
Packit 345191
#
Packit 345191
#   The second argument, if specified, indicates whether you insist on an
Packit 345191
#   extended mode (e.g. -std=gnu++11) or a strict conformance mode (e.g.
Packit 345191
#   -std=c++11).  If neither is specified, you get whatever works, with
Packit 345191
#   preference for an extended mode.
Packit 345191
#
Packit 345191
#   The third argument, if specified 'mandatory' or if left unspecified,
Packit 345191
#   indicates that baseline support for the specified C++ standard is
Packit 345191
#   required and that the macro should error out if no mode with that
Packit 345191
#   support is found.  If specified 'optional', then configuration proceeds
Packit 345191
#   regardless, after defining HAVE_CXX${VERSION} if and only if a
Packit 345191
#   supporting mode is found.
Packit 345191
#
Packit 345191
# LICENSE
Packit 345191
#
Packit 345191
#   Copyright (c) 2008 Benjamin Kosnik <bkoz@redhat.com>
Packit 345191
#   Copyright (c) 2012 Zack Weinberg <zackw@panix.com>
Packit 345191
#   Copyright (c) 2013 Roy Stogner <roystgnr@ices.utexas.edu>
Packit 345191
#   Copyright (c) 2014, 2015 Google Inc.; contributed by Alexey Sokolov <sokolov@google.com>
Packit 345191
#   Copyright (c) 2015 Paul Norman <penorman@mac.com>
Packit 345191
#   Copyright (c) 2015 Moritz Klammler <moritz@klammler.eu>
Packit 345191
#
Packit 345191
#   Copying and distribution of this file, with or without modification, are
Packit 345191
#   permitted in any medium without royalty provided the copyright notice
Packit 345191
#   and this notice are preserved.  This file is offered as-is, without any
Packit 345191
#   warranty.
Packit 345191
Packit 345191
#serial 4
Packit 345191
Packit 345191
dnl  This macro is based on the code from the AX_CXX_COMPILE_STDCXX_11 macro
Packit 345191
dnl  (serial version number 13).
Packit 345191
Packit 345191
AC_DEFUN([AX_CXX_COMPILE_STDCXX], [dnl
Packit 345191
  m4_if([$1], [11], [],
Packit 345191
        [$1], [14], [],
Packit 345191
        [$1], [17], [m4_fatal([support for C++17 not yet implemented in AX_CXX_COMPILE_STDCXX])],
Packit 345191
        [m4_fatal([invalid first argument `$1' to AX_CXX_COMPILE_STDCXX])])dnl
Packit 345191
  m4_if([$2], [], [],
Packit 345191
        [$2], [ext], [],
Packit 345191
        [$2], [noext], [],
Packit 345191
        [m4_fatal([invalid second argument `$2' to AX_CXX_COMPILE_STDCXX])])dnl
Packit 345191
  m4_if([$3], [], [ax_cxx_compile_cxx$1_required=true],
Packit 345191
        [$3], [mandatory], [ax_cxx_compile_cxx$1_required=true],
Packit 345191
        [$3], [optional], [ax_cxx_compile_cxx$1_required=false],
Packit 345191
        [m4_fatal([invalid third argument `$3' to AX_CXX_COMPILE_STDCXX])])
Packit 345191
  AC_LANG_PUSH([C++])dnl
Packit 345191
  ac_success=no
Packit 345191
  AC_CACHE_CHECK(whether $CXX supports C++$1 features by default,
Packit 345191
  ax_cv_cxx_compile_cxx$1,
Packit 345191
  [AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],
Packit 345191
    [ax_cv_cxx_compile_cxx$1=yes],
Packit 345191
    [ax_cv_cxx_compile_cxx$1=no])])
Packit 345191
  if test x$ax_cv_cxx_compile_cxx$1 = xyes; then
Packit 345191
    ac_success=yes
Packit 345191
  fi
Packit 345191
Packit 345191
  m4_if([$2], [noext], [], [dnl
Packit 345191
  if test x$ac_success = xno; then
Packit 345191
    for switch in -std=gnu++$1 -std=gnu++0x; do
Packit 345191
      cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_$switch])
Packit 345191
      AC_CACHE_CHECK(whether $CXX supports C++$1 features with $switch,
Packit 345191
                     $cachevar,
Packit 345191
        [ac_save_CXX="$CXX"
Packit 345191
         CXX="$CXX $switch"
Packit 345191
         AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],
Packit 345191
          [eval $cachevar=yes],
Packit 345191
          [eval $cachevar=no])
Packit 345191
         CXX="$ac_save_CXX"])
Packit 345191
      if eval test x\$$cachevar = xyes; then
Packit 345191
        CXX="$CXX $switch"
Packit 345191
        if test -n "$CXXCPP" ; then
Packit 345191
          CXXCPP="$CXXCPP $switch"
Packit 345191
        fi
Packit 345191
        ac_success=yes
Packit 345191
        break
Packit 345191
      fi
Packit 345191
    done
Packit 345191
  fi])
Packit 345191
Packit 345191
  m4_if([$2], [ext], [], [dnl
Packit 345191
  if test x$ac_success = xno; then
Packit 345191
    dnl HP's aCC needs +std=c++11 according to:
Packit 345191
    dnl http://h21007.www2.hp.com/portal/download/files/unprot/aCxx/PDF_Release_Notes/769149-001.pdf
Packit 345191
    dnl Cray's crayCC needs "-h std=c++11"
Packit 345191
    for switch in -std=c++$1 -std=c++0x +std=c++$1 "-h std=c++$1"; do
Packit 345191
      cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_$switch])
Packit 345191
      AC_CACHE_CHECK(whether $CXX supports C++$1 features with $switch,
Packit 345191
                     $cachevar,
Packit 345191
        [ac_save_CXX="$CXX"
Packit 345191
         CXX="$CXX $switch"
Packit 345191
         AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],
Packit 345191
          [eval $cachevar=yes],
Packit 345191
          [eval $cachevar=no])
Packit 345191
         CXX="$ac_save_CXX"])
Packit 345191
      if eval test x\$$cachevar = xyes; then
Packit 345191
        CXX="$CXX $switch"
Packit 345191
        if test -n "$CXXCPP" ; then
Packit 345191
          CXXCPP="$CXXCPP $switch"
Packit 345191
        fi
Packit 345191
        ac_success=yes
Packit 345191
        break
Packit 345191
      fi
Packit 345191
    done
Packit 345191
  fi])
Packit 345191
  AC_LANG_POP([C++])
Packit 345191
  if test x$ax_cxx_compile_cxx$1_required = xtrue; then
Packit 345191
    if test x$ac_success = xno; then
Packit 345191
      AC_MSG_ERROR([*** A compiler with support for C++$1 language features is required.])
Packit 345191
    fi
Packit 345191
  fi
Packit 345191
  if test x$ac_success = xno; then
Packit 345191
    HAVE_CXX$1=0
Packit 345191
    AC_MSG_NOTICE([No compiler with C++$1 support was found])
Packit 345191
  else
Packit 345191
    HAVE_CXX$1=1
Packit 345191
    AC_DEFINE(HAVE_CXX$1,1,
Packit 345191
              [define if the compiler supports basic C++$1 syntax])
Packit 345191
  fi
Packit 345191
  AC_SUBST(HAVE_CXX$1)
Packit 345191
])
Packit 345191
Packit 345191
Packit 345191
dnl  Test body for checking C++11 support
Packit 345191
Packit 345191
m4_define([_AX_CXX_COMPILE_STDCXX_testbody_11],
Packit 345191
  _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
Packit 345191
)
Packit 345191
Packit 345191
Packit 345191
dnl  Test body for checking C++14 support
Packit 345191
Packit 345191
m4_define([_AX_CXX_COMPILE_STDCXX_testbody_14],
Packit 345191
  _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
Packit 345191
  _AX_CXX_COMPILE_STDCXX_testbody_new_in_14
Packit 345191
)
Packit 345191
Packit 345191
Packit 345191
dnl  Tests for new features in C++11
Packit 345191
Packit 345191
m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_11], [[
Packit 345191
Packit 345191
// If the compiler admits that it is not ready for C++11, why torture it?
Packit 345191
// Hopefully, this will speed up the test.
Packit 345191
Packit 345191
#ifndef __cplusplus
Packit 345191
Packit 345191
#error "This is not a C++ compiler"
Packit 345191
Packit 345191
#elif __cplusplus < 201103L
Packit 345191
Packit 345191
#error "This is not a C++11 compiler"
Packit 345191
Packit 345191
#else
Packit 345191
Packit 345191
namespace cxx11
Packit 345191
{
Packit 345191
Packit 345191
  namespace test_static_assert
Packit 345191
  {
Packit 345191
Packit 345191
    template <typename T>
Packit 345191
    struct check
Packit 345191
    {
Packit 345191
      static_assert(sizeof(int) <= sizeof(T), "not big enough");
Packit 345191
    };
Packit 345191
Packit 345191
  }
Packit 345191
Packit 345191
  namespace test_final_override
Packit 345191
  {
Packit 345191
Packit 345191
    struct Base
Packit 345191
    {
Packit 345191
      virtual void f() {}
Packit 345191
    };
Packit 345191
Packit 345191
    struct Derived : public Base
Packit 345191
    {
Packit 345191
      virtual void f() override {}
Packit 345191
    };
Packit 345191
Packit 345191
  }
Packit 345191
Packit 345191
  namespace test_double_right_angle_brackets
Packit 345191
  {
Packit 345191
Packit 345191
    template < typename T >
Packit 345191
    struct check {};
Packit 345191
Packit 345191
    typedef check<void> single_type;
Packit 345191
    typedef check<check<void>> double_type;
Packit 345191
    typedef check<check<check<void>>> triple_type;
Packit 345191
    typedef check<check<check<check<void>>>> quadruple_type;
Packit 345191
Packit 345191
  }
Packit 345191
Packit 345191
  namespace test_decltype
Packit 345191
  {
Packit 345191
Packit 345191
    int
Packit 345191
    f()
Packit 345191
    {
Packit 345191
      int a = 1;
Packit 345191
      decltype(a) b = 2;
Packit 345191
      return a + b;
Packit 345191
    }
Packit 345191
Packit 345191
  }
Packit 345191
Packit 345191
  namespace test_type_deduction
Packit 345191
  {
Packit 345191
Packit 345191
    template < typename T1, typename T2 >
Packit 345191
    struct is_same
Packit 345191
    {
Packit 345191
      static const bool value = false;
Packit 345191
    };
Packit 345191
Packit 345191
    template < typename T >
Packit 345191
    struct is_same<T, T>
Packit 345191
    {
Packit 345191
      static const bool value = true;
Packit 345191
    };
Packit 345191
Packit 345191
    template < typename T1, typename T2 >
Packit 345191
    auto
Packit 345191
    add(T1 a1, T2 a2) -> decltype(a1 + a2)
Packit 345191
    {
Packit 345191
      return a1 + a2;
Packit 345191
    }
Packit 345191
Packit 345191
    int
Packit 345191
    test(const int c, volatile int v)
Packit 345191
    {
Packit 345191
      static_assert(is_same<int, decltype(0)>::value == true, "");
Packit 345191
      static_assert(is_same<int, decltype(c)>::value == false, "");
Packit 345191
      static_assert(is_same<int, decltype(v)>::value == false, "");
Packit 345191
      auto ac = c;
Packit 345191
      auto av = v;
Packit 345191
      auto sumi = ac + av + 'x';
Packit 345191
      auto sumf = ac + av + 1.0;
Packit 345191
      static_assert(is_same<int, decltype(ac)>::value == true, "");
Packit 345191
      static_assert(is_same<int, decltype(av)>::value == true, "");
Packit 345191
      static_assert(is_same<int, decltype(sumi)>::value == true, "");
Packit 345191
      static_assert(is_same<int, decltype(sumf)>::value == false, "");
Packit 345191
      static_assert(is_same<int, decltype(add(c, v))>::value == true, "");
Packit 345191
      return (sumf > 0.0) ? sumi : add(c, v);
Packit 345191
    }
Packit 345191
Packit 345191
  }
Packit 345191
Packit 345191
  namespace test_noexcept
Packit 345191
  {
Packit 345191
Packit 345191
    int f() { return 0; }
Packit 345191
    int g() noexcept { return 0; }
Packit 345191
Packit 345191
    static_assert(noexcept(f()) == false, "");
Packit 345191
    static_assert(noexcept(g()) == true, "");
Packit 345191
Packit 345191
  }
Packit 345191
Packit 345191
  namespace test_constexpr
Packit 345191
  {
Packit 345191
Packit 345191
    template < typename CharT >
Packit 345191
    unsigned long constexpr
Packit 345191
    strlen_c_r(const CharT *const s, const unsigned long acc) noexcept
Packit 345191
    {
Packit 345191
      return *s ? strlen_c_r(s + 1, acc + 1) : acc;
Packit 345191
    }
Packit 345191
Packit 345191
    template < typename CharT >
Packit 345191
    unsigned long constexpr
Packit 345191
    strlen_c(const CharT *const s) noexcept
Packit 345191
    {
Packit 345191
      return strlen_c_r(s, 0UL);
Packit 345191
    }
Packit 345191
Packit 345191
    static_assert(strlen_c("") == 0UL, "");
Packit 345191
    static_assert(strlen_c("1") == 1UL, "");
Packit 345191
    static_assert(strlen_c("example") == 7UL, "");
Packit 345191
    static_assert(strlen_c("another\0example") == 7UL, "");
Packit 345191
Packit 345191
  }
Packit 345191
Packit 345191
  namespace test_rvalue_references
Packit 345191
  {
Packit 345191
Packit 345191
    template < int N >
Packit 345191
    struct answer
Packit 345191
    {
Packit 345191
      static constexpr int value = N;
Packit 345191
    };
Packit 345191
Packit 345191
    answer<1> f(int&)       { return answer<1>(); }
Packit 345191
    answer<2> f(const int&) { return answer<2>(); }
Packit 345191
    answer<3> f(int&&)      { return answer<3>(); }
Packit 345191
Packit 345191
    void
Packit 345191
    test()
Packit 345191
    {
Packit 345191
      int i = 0;
Packit 345191
      const int c = 0;
Packit 345191
      static_assert(decltype(f(i))::value == 1, "");
Packit 345191
      static_assert(decltype(f(c))::value == 2, "");
Packit 345191
      static_assert(decltype(f(0))::value == 3, "");
Packit 345191
    }
Packit 345191
Packit 345191
  }
Packit 345191
Packit 345191
  namespace test_uniform_initialization
Packit 345191
  {
Packit 345191
Packit 345191
    struct test
Packit 345191
    {
Packit 345191
      static const int zero {};
Packit 345191
      static const int one {1};
Packit 345191
    };
Packit 345191
Packit 345191
    static_assert(test::zero == 0, "");
Packit 345191
    static_assert(test::one == 1, "");
Packit 345191
Packit 345191
  }
Packit 345191
Packit 345191
  namespace test_lambdas
Packit 345191
  {
Packit 345191
Packit 345191
    void
Packit 345191
    test1()
Packit 345191
    {
Packit 345191
      auto lambda1 = [](){};
Packit 345191
      auto lambda2 = lambda1;
Packit 345191
      lambda1();
Packit 345191
      lambda2();
Packit 345191
    }
Packit 345191
Packit 345191
    int
Packit 345191
    test2()
Packit 345191
    {
Packit 345191
      auto a = [](int i, int j){ return i + j; }(1, 2);
Packit 345191
      auto b = []() -> int { return '0'; }();
Packit 345191
      auto c = [=](){ return a + b; }();
Packit 345191
      auto d = [&](){ return c; }();
Packit 345191
      auto e = [a, &b](int x) mutable {
Packit 345191
        const auto identity = [](int y){ return y; };
Packit 345191
        for (auto i = 0; i < a; ++i)
Packit 345191
          a += b--;
Packit 345191
        return x + identity(a + b);
Packit 345191
      }(0);
Packit 345191
      return a + b + c + d + e;
Packit 345191
    }
Packit 345191
Packit 345191
    int
Packit 345191
    test3()
Packit 345191
    {
Packit 345191
      const auto nullary = [](){ return 0; };
Packit 345191
      const auto unary = [](int x){ return x; };
Packit 345191
      using nullary_t = decltype(nullary);
Packit 345191
      using unary_t = decltype(unary);
Packit 345191
      const auto higher1st = [](nullary_t f){ return f(); };
Packit 345191
      const auto higher2nd = [unary](nullary_t f1){
Packit 345191
        return [unary, f1](unary_t f2){ return f2(unary(f1())); };
Packit 345191
      };
Packit 345191
      return higher1st(nullary) + higher2nd(nullary)(unary);
Packit 345191
    }
Packit 345191
Packit 345191
  }
Packit 345191
Packit 345191
  namespace test_variadic_templates
Packit 345191
  {
Packit 345191
Packit 345191
    template <int...>
Packit 345191
    struct sum;
Packit 345191
Packit 345191
    template <int N0, int... N1toN>
Packit 345191
    struct sum<N0, N1toN...>
Packit 345191
    {
Packit 345191
      static constexpr auto value = N0 + sum<N1toN...>::value;
Packit 345191
    };
Packit 345191
Packit 345191
    template <>
Packit 345191
    struct sum<>
Packit 345191
    {
Packit 345191
      static constexpr auto value = 0;
Packit 345191
    };
Packit 345191
Packit 345191
    static_assert(sum<>::value == 0, "");
Packit 345191
    static_assert(sum<1>::value == 1, "");
Packit 345191
    static_assert(sum<23>::value == 23, "");
Packit 345191
    static_assert(sum<1, 2>::value == 3, "");
Packit 345191
    static_assert(sum<5, 5, 11>::value == 21, "");
Packit 345191
    static_assert(sum<2, 3, 5, 7, 11, 13>::value == 41, "");
Packit 345191
Packit 345191
  }
Packit 345191
Packit 345191
  // http://stackoverflow.com/questions/13728184/template-aliases-and-sfinae
Packit 345191
  // Clang 3.1 fails with headers of libstd++ 4.8.3 when using std::function
Packit 345191
  // because of this.
Packit 345191
  namespace test_template_alias_sfinae
Packit 345191
  {
Packit 345191
Packit 345191
    struct foo {};
Packit 345191
Packit 345191
    template<typename T>
Packit 345191
    using member = typename T::member_type;
Packit 345191
Packit 345191
    template<typename T>
Packit 345191
    void func(...) {}
Packit 345191
Packit 345191
    template<typename T>
Packit 345191
    void func(member<T>*) {}
Packit 345191
Packit 345191
    void test();
Packit 345191
Packit 345191
    void test() { func<foo>(0); }
Packit 345191
Packit 345191
  }
Packit 345191
Packit 345191
}  // namespace cxx11
Packit 345191
Packit 345191
#endif  // __cplusplus >= 201103L
Packit 345191
Packit 345191
]])
Packit 345191
Packit 345191
Packit 345191
dnl  Tests for new features in C++14
Packit 345191
Packit 345191
m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_14], [[
Packit 345191
Packit 345191
// If the compiler admits that it is not ready for C++14, why torture it?
Packit 345191
// Hopefully, this will speed up the test.
Packit 345191
Packit 345191
#ifndef __cplusplus
Packit 345191
Packit 345191
#error "This is not a C++ compiler"
Packit 345191
Packit 345191
#elif __cplusplus < 201402L
Packit 345191
Packit 345191
#error "This is not a C++14 compiler"
Packit 345191
Packit 345191
#else
Packit 345191
Packit 345191
namespace cxx14
Packit 345191
{
Packit 345191
Packit 345191
  namespace test_polymorphic_lambdas
Packit 345191
  {
Packit 345191
Packit 345191
    int
Packit 345191
    test()
Packit 345191
    {
Packit 345191
      const auto lambda = [](auto&&... args){
Packit 345191
        const auto istiny = [](auto x){
Packit 345191
          return (sizeof(x) == 1UL) ? 1 : 0;
Packit 345191
        };
Packit 345191
        const int aretiny[] = { istiny(args)... };
Packit 345191
        return aretiny[0];
Packit 345191
      };
Packit 345191
      return lambda(1, 1L, 1.0f, '1');
Packit 345191
    }
Packit 345191
Packit 345191
  }
Packit 345191
Packit 345191
  namespace test_binary_literals
Packit 345191
  {
Packit 345191
Packit 345191
    constexpr auto ivii = 0b0000000000101010;
Packit 345191
    static_assert(ivii == 42, "wrong value");
Packit 345191
Packit 345191
  }
Packit 345191
Packit 345191
  namespace test_generalized_constexpr
Packit 345191
  {
Packit 345191
Packit 345191
    template < typename CharT >
Packit 345191
    constexpr unsigned long
Packit 345191
    strlen_c(const CharT *const s) noexcept
Packit 345191
    {
Packit 345191
      auto length = 0UL;
Packit 345191
      for (auto p = s; *p; ++p)
Packit 345191
        ++length;
Packit 345191
      return length;
Packit 345191
    }
Packit 345191
Packit 345191
    static_assert(strlen_c("") == 0UL, "");
Packit 345191
    static_assert(strlen_c("x") == 1UL, "");
Packit 345191
    static_assert(strlen_c("test") == 4UL, "");
Packit 345191
    static_assert(strlen_c("another\0test") == 7UL, "");
Packit 345191
Packit 345191
  }
Packit 345191
Packit 345191
  namespace test_lambda_init_capture
Packit 345191
  {
Packit 345191
Packit 345191
    int
Packit 345191
    test()
Packit 345191
    {
Packit 345191
      auto x = 0;
Packit 345191
      const auto lambda1 = [a = x](int b){ return a + b; };
Packit 345191
      const auto lambda2 = [a = lambda1(x)](){ return a; };
Packit 345191
      return lambda2();
Packit 345191
    }
Packit 345191
Packit 345191
  }
Packit 345191
Packit 345191
  namespace test_digit_seperators
Packit 345191
  {
Packit 345191
Packit 345191
    constexpr auto ten_million = 100'000'000;
Packit 345191
    static_assert(ten_million == 100000000, "");
Packit 345191
Packit 345191
  }
Packit 345191
Packit 345191
  namespace test_return_type_deduction
Packit 345191
  {
Packit 345191
Packit 345191
    auto f(int& x) { return x; }
Packit 345191
    decltype(auto) g(int& x) { return x; }
Packit 345191
Packit 345191
    template < typename T1, typename T2 >
Packit 345191
    struct is_same
Packit 345191
    {
Packit 345191
      static constexpr auto value = false;
Packit 345191
    };
Packit 345191
Packit 345191
    template < typename T >
Packit 345191
    struct is_same<T, T>
Packit 345191
    {
Packit 345191
      static constexpr auto value = true;
Packit 345191
    };
Packit 345191
Packit 345191
    int
Packit 345191
    test()
Packit 345191
    {
Packit 345191
      auto x = 0;
Packit 345191
      static_assert(is_same<int, decltype(f(x))>::value, "");
Packit 345191
      static_assert(is_same<int&, decltype(g(x))>::value, "");
Packit 345191
      return x;
Packit 345191
    }
Packit 345191
Packit 345191
  }
Packit 345191
Packit 345191
}  // namespace cxx14
Packit 345191
Packit 345191
#endif  // __cplusplus >= 201402L
Packit 345191
Packit 345191
]])