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