| 1 |
# |
| 2 |
# The version of this file from the last packaged version of |
| 3 |
# autoconf-archive doesn't include checks for C++23 and older |
| 4 |
# versions of autoconf-archive such as those in Rocky 8 and CentOS 7 |
| 5 |
# only handle up to C++17. The version in the autoconf source repo |
| 6 |
# however does handle up to C++23 so we're including it here. |
| 7 |
# |
| 8 |
# =========================================================================== |
| 9 |
# https://www.gnu.org/software/autoconf-archive/ax_cxx_compile_stdcxx.html |
| 10 |
# =========================================================================== |
| 11 |
# |
| 12 |
# SYNOPSIS |
| 13 |
# |
| 14 |
# AX_CXX_COMPILE_STDCXX(VERSION, [ext|noext], [mandatory|optional]) |
| 15 |
# |
| 16 |
# DESCRIPTION |
| 17 |
# |
| 18 |
# Check for baseline language coverage in the compiler for the specified |
| 19 |
# version of the C++ standard. If necessary, add switches to CXX and |
| 20 |
# CXXCPP to enable support. VERSION may be '11', '14', '17', '20', or |
| 21 |
# '23' for the respective C++ standard version. |
| 22 |
# |
| 23 |
# The second argument, if specified, indicates whether you insist on an |
| 24 |
# extended mode (e.g. -std=gnu++11) or a strict conformance mode (e.g. |
| 25 |
# -std=c++11). If neither is specified, you get whatever works, with |
| 26 |
# preference for no added switch, and then for an extended mode. |
| 27 |
# |
| 28 |
# The third argument, if specified 'mandatory' or if left unspecified, |
| 29 |
# indicates that baseline support for the specified C++ standard is |
| 30 |
# required and that the macro should error out if no mode with that |
| 31 |
# support is found. If specified 'optional', then configuration proceeds |
| 32 |
# regardless, after defining HAVE_CXX${VERSION} if and only if a |
| 33 |
# supporting mode is found. |
| 34 |
# |
| 35 |
# LICENSE |
| 36 |
# |
| 37 |
# Copyright (c) 2008 Benjamin Kosnik <[email protected]> |
| 38 |
# Copyright (c) 2012 Zack Weinberg <[email protected]> |
| 39 |
# Copyright (c) 2013 Roy Stogner <[email protected]> |
| 40 |
# Copyright (c) 2014, 2015 Google Inc.; contributed by Alexey Sokolov <[email protected]> |
| 41 |
# Copyright (c) 2015 Paul Norman <[email protected]> |
| 42 |
# Copyright (c) 2015 Moritz Klammler <[email protected]> |
| 43 |
# Copyright (c) 2016, 2018 Krzesimir Nowak <[email protected]> |
| 44 |
# Copyright (c) 2019 Enji Cooper <[email protected]> |
| 45 |
# Copyright (c) 2020 Jason Merrill <[email protected]> |
| 46 |
# Copyright (c) 2021, 2024 Jörn Heusipp <[email protected]> |
| 47 |
# Copyright (c) 2015, 2022, 2023, 2024 Olly Betts |
| 48 |
# |
| 49 |
# Copying and distribution of this file, with or without modification, are |
| 50 |
# permitted in any medium without royalty provided the copyright notice |
| 51 |
# and this notice are preserved. This file is offered as-is, without any |
| 52 |
# warranty. |
| 53 |
|
| 54 |
#serial 25 |
| 55 |
|
| 56 |
dnl This macro is based on the code from the AX_CXX_COMPILE_STDCXX_11 macro |
| 57 |
dnl (serial version number 13). |
| 58 |
|
| 59 |
AC_DEFUN([AX_CXX_COMPILE_STDCXX], [dnl |
| 60 |
m4_if([$1], [11], [ax_cxx_compile_alternatives="11 0x"], |
| 61 |
[$1], [14], [ax_cxx_compile_alternatives="14 1y"], |
| 62 |
[$1], [17], [ax_cxx_compile_alternatives="17 1z"], |
| 63 |
[$1], [20], [ax_cxx_compile_alternatives="20"], |
| 64 |
[$1], [23], [ax_cxx_compile_alternatives="23"], |
| 65 |
[m4_fatal([invalid first argument `$1' to AX_CXX_COMPILE_STDCXX])])dnl |
| 66 |
m4_if([$2], [], [], |
| 67 |
[$2], [ext], [], |
| 68 |
[$2], [noext], [], |
| 69 |
[m4_fatal([invalid second argument `$2' to AX_CXX_COMPILE_STDCXX])])dnl |
| 70 |
m4_if([$3], [], [ax_cxx_compile_cxx$1_required=true], |
| 71 |
[$3], [mandatory], [ax_cxx_compile_cxx$1_required=true], |
| 72 |
[$3], [optional], [ax_cxx_compile_cxx$1_required=false], |
| 73 |
[m4_fatal([invalid third argument `$3' to AX_CXX_COMPILE_STDCXX])]) |
| 74 |
AC_LANG_PUSH([C++])dnl |
| 75 |
ac_success=no |
| 76 |
|
| 77 |
m4_if([$2], [], [dnl |
| 78 |
AC_CACHE_CHECK(whether $CXX supports C++$1 features by default, |
| 79 |
ax_cv_cxx_compile_cxx$1, |
| 80 |
[AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])], |
| 81 |
[ax_cv_cxx_compile_cxx$1=yes], |
| 82 |
[ax_cv_cxx_compile_cxx$1=no])]) |
| 83 |
if test x$ax_cv_cxx_compile_cxx$1 = xyes; then |
| 84 |
ac_success=yes |
| 85 |
fi]) |
| 86 |
|
| 87 |
m4_if([$2], [noext], [], [dnl |
| 88 |
if test x$ac_success = xno; then |
| 89 |
for alternative in ${ax_cxx_compile_alternatives}; do |
| 90 |
switch="-std=gnu++${alternative}" |
| 91 |
cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_$switch]) |
| 92 |
AC_CACHE_CHECK(whether $CXX supports C++$1 features with $switch, |
| 93 |
$cachevar, |
| 94 |
[ac_save_CXX="$CXX" |
| 95 |
CXX="$CXX $switch" |
| 96 |
AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])], |
| 97 |
[eval $cachevar=yes], |
| 98 |
[eval $cachevar=no]) |
| 99 |
CXX="$ac_save_CXX"]) |
| 100 |
if eval test x\$$cachevar = xyes; then |
| 101 |
CXX="$CXX $switch" |
| 102 |
if test -n "$CXXCPP" ; then |
| 103 |
CXXCPP="$CXXCPP $switch" |
| 104 |
fi |
| 105 |
ac_success=yes |
| 106 |
break |
| 107 |
fi |
| 108 |
done |
| 109 |
fi]) |
| 110 |
|
| 111 |
m4_if([$2], [ext], [], [dnl |
| 112 |
if test x$ac_success = xno; then |
| 113 |
dnl HP's aCC needs +std=c++11 according to: |
| 114 |
dnl http://h21007.www2.hp.com/portal/download/files/unprot/aCxx/PDF_Release_Notes/769149-001.pdf |
| 115 |
dnl Cray's crayCC needs "-h std=c++11" |
| 116 |
dnl MSVC needs -std:c++NN for C++17 and later (default is C++14) |
| 117 |
for alternative in ${ax_cxx_compile_alternatives}; do |
| 118 |
for switch in -std=c++${alternative} +std=c++${alternative} "-h std=c++${alternative}" MSVC; do |
| 119 |
if test x"$switch" = xMSVC; then |
| 120 |
dnl AS_TR_SH maps both `:` and `=` to `_` so -std:c++17 would collide |
| 121 |
dnl with -std=c++17. We suffix the cache variable name with _MSVC to |
| 122 |
dnl avoid this. |
| 123 |
switch=-std:c++${alternative} |
| 124 |
cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_${switch}_MSVC]) |
| 125 |
else |
| 126 |
cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_$switch]) |
| 127 |
fi |
| 128 |
AC_CACHE_CHECK(whether $CXX supports C++$1 features with $switch, |
| 129 |
$cachevar, |
| 130 |
[ac_save_CXX="$CXX" |
| 131 |
CXX="$CXX $switch" |
| 132 |
AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])], |
| 133 |
[eval $cachevar=yes], |
| 134 |
[eval $cachevar=no]) |
| 135 |
CXX="$ac_save_CXX"]) |
| 136 |
if eval test x\$$cachevar = xyes; then |
| 137 |
CXX="$CXX $switch" |
| 138 |
if test -n "$CXXCPP" ; then |
| 139 |
CXXCPP="$CXXCPP $switch" |
| 140 |
fi |
| 141 |
ac_success=yes |
| 142 |
break |
| 143 |
fi |
| 144 |
done |
| 145 |
if test x$ac_success = xyes; then |
| 146 |
break |
| 147 |
fi |
| 148 |
done |
| 149 |
fi]) |
| 150 |
AC_LANG_POP([C++]) |
| 151 |
if test x$ax_cxx_compile_cxx$1_required = xtrue; then |
| 152 |
if test x$ac_success = xno; then |
| 153 |
AC_MSG_ERROR([*** A compiler with support for C++$1 language features is required.]) |
| 154 |
fi |
| 155 |
fi |
| 156 |
if test x$ac_success = xno; then |
| 157 |
HAVE_CXX$1=0 |
| 158 |
AC_MSG_NOTICE([No compiler with C++$1 support was found]) |
| 159 |
else |
| 160 |
HAVE_CXX$1=1 |
| 161 |
AC_DEFINE(HAVE_CXX$1,1, |
| 162 |
[define if the compiler supports basic C++$1 syntax]) |
| 163 |
fi |
| 164 |
AC_SUBST(HAVE_CXX$1) |
| 165 |
]) |
| 166 |
|
| 167 |
|
| 168 |
dnl Test body for checking C++11 support |
| 169 |
|
| 170 |
m4_define([_AX_CXX_COMPILE_STDCXX_testbody_11], |
| 171 |
[_AX_CXX_COMPILE_STDCXX_testbody_new_in_11] |
| 172 |
) |
| 173 |
|
| 174 |
dnl Test body for checking C++14 support |
| 175 |
|
| 176 |
m4_define([_AX_CXX_COMPILE_STDCXX_testbody_14], |
| 177 |
[_AX_CXX_COMPILE_STDCXX_testbody_new_in_11 |
| 178 |
_AX_CXX_COMPILE_STDCXX_testbody_new_in_14] |
| 179 |
) |
| 180 |
|
| 181 |
dnl Test body for checking C++17 support |
| 182 |
|
| 183 |
m4_define([_AX_CXX_COMPILE_STDCXX_testbody_17], |
| 184 |
[_AX_CXX_COMPILE_STDCXX_testbody_new_in_11 |
| 185 |
_AX_CXX_COMPILE_STDCXX_testbody_new_in_14 |
| 186 |
_AX_CXX_COMPILE_STDCXX_testbody_new_in_17] |
| 187 |
) |
| 188 |
|
| 189 |
dnl Test body for checking C++20 support |
| 190 |
|
| 191 |
m4_define([_AX_CXX_COMPILE_STDCXX_testbody_20], |
| 192 |
[_AX_CXX_COMPILE_STDCXX_testbody_new_in_11 |
| 193 |
_AX_CXX_COMPILE_STDCXX_testbody_new_in_14 |
| 194 |
_AX_CXX_COMPILE_STDCXX_testbody_new_in_17 |
| 195 |
_AX_CXX_COMPILE_STDCXX_testbody_new_in_20] |
| 196 |
) |
| 197 |
|
| 198 |
dnl Test body for checking C++23 support |
| 199 |
|
| 200 |
m4_define([_AX_CXX_COMPILE_STDCXX_testbody_23], |
| 201 |
[_AX_CXX_COMPILE_STDCXX_testbody_new_in_11 |
| 202 |
_AX_CXX_COMPILE_STDCXX_testbody_new_in_14 |
| 203 |
_AX_CXX_COMPILE_STDCXX_testbody_new_in_17 |
| 204 |
_AX_CXX_COMPILE_STDCXX_testbody_new_in_20 |
| 205 |
_AX_CXX_COMPILE_STDCXX_testbody_new_in_23] |
| 206 |
) |
| 207 |
|
| 208 |
|
| 209 |
dnl Tests for new features in C++11 |
| 210 |
|
| 211 |
m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_11], [[ |
| 212 |
|
| 213 |
// If the compiler admits that it is not ready for C++11, why torture it? |
| 214 |
// Hopefully, this will speed up the test. |
| 215 |
|
| 216 |
#ifndef __cplusplus |
| 217 |
|
| 218 |
#error "This is not a C++ compiler" |
| 219 |
|
| 220 |
// MSVC always sets __cplusplus to 199711L in older versions; newer versions |
| 221 |
// only set it correctly if /Zc:__cplusplus is specified as well as a |
| 222 |
// /std:c++NN switch: |
| 223 |
// |
| 224 |
// https://devblogs.microsoft.com/cppblog/msvc-now-correctly-reports-__cplusplus/ |
| 225 |
// |
| 226 |
// The value __cplusplus ought to have is available in _MSVC_LANG since |
| 227 |
// Visual Studio 2015 Update 3: |
| 228 |
// |
| 229 |
// https://learn.microsoft.com/en-us/cpp/preprocessor/predefined-macros |
| 230 |
// |
| 231 |
// This was also the first MSVC version to support C++14 so we can't use the |
| 232 |
// value of either __cplusplus or _MSVC_LANG to quickly rule out MSVC having |
| 233 |
// C++11 or C++14 support, but we can check _MSVC_LANG for C++17 and later. |
| 234 |
#elif __cplusplus < 201103L && !defined _MSC_VER |
| 235 |
|
| 236 |
#error "This is not a C++11 compiler" |
| 237 |
|
| 238 |
#else |
| 239 |
|
| 240 |
namespace cxx11 |
| 241 |
{ |
| 242 |
|
| 243 |
namespace test_static_assert |
| 244 |
{ |
| 245 |
|
| 246 |
template <typename T> |
| 247 |
struct check |
| 248 |
{ |
| 249 |
static_assert(sizeof(int) <= sizeof(T), "not big enough"); |
| 250 |
}; |
| 251 |
|
| 252 |
} |
| 253 |
|
| 254 |
namespace test_final_override |
| 255 |
{ |
| 256 |
|
| 257 |
struct Base |
| 258 |
{ |
| 259 |
virtual ~Base() {} |
| 260 |
virtual void f() {} |
| 261 |
}; |
| 262 |
|
| 263 |
struct Derived : public Base |
| 264 |
{ |
| 265 |
virtual ~Derived() override {} |
| 266 |
virtual void f() override {} |
| 267 |
}; |
| 268 |
|
| 269 |
} |
| 270 |
|
| 271 |
namespace test_double_right_angle_brackets |
| 272 |
{ |
| 273 |
|
| 274 |
template < typename T > |
| 275 |
struct check {}; |
| 276 |
|
| 277 |
typedef check<void> single_type; |
| 278 |
typedef check<check<void>> double_type; |
| 279 |
typedef check<check<check<void>>> triple_type; |
| 280 |
typedef check<check<check<check<void>>>> quadruple_type; |
| 281 |
|
| 282 |
} |
| 283 |
|
| 284 |
namespace test_decltype |
| 285 |
{ |
| 286 |
|
| 287 |
int |
| 288 |
f() |
| 289 |
{ |
| 290 |
int a = 1; |
| 291 |
decltype(a) b = 2; |
| 292 |
return a + b; |
| 293 |
} |
| 294 |
|
| 295 |
} |
| 296 |
|
| 297 |
namespace test_type_deduction |
| 298 |
{ |
| 299 |
|
| 300 |
template < typename T1, typename T2 > |
| 301 |
struct is_same |
| 302 |
{ |
| 303 |
static const bool value = false; |
| 304 |
}; |
| 305 |
|
| 306 |
template < typename T > |
| 307 |
struct is_same<T, T> |
| 308 |
{ |
| 309 |
static const bool value = true; |
| 310 |
}; |
| 311 |
|
| 312 |
template < typename T1, typename T2 > |
| 313 |
auto |
| 314 |
add(T1 a1, T2 a2) -> decltype(a1 + a2) |
| 315 |
{ |
| 316 |
return a1 + a2; |
| 317 |
} |
| 318 |
|
| 319 |
int |
| 320 |
test(const int c, volatile int v) |
| 321 |
{ |
| 322 |
static_assert(is_same<int, decltype(0)>::value == true, ""); |
| 323 |
static_assert(is_same<int, decltype(c)>::value == false, ""); |
| 324 |
static_assert(is_same<int, decltype(v)>::value == false, ""); |
| 325 |
auto ac = c; |
| 326 |
auto av = v; |
| 327 |
auto sumi = ac + av + 'x'; |
| 328 |
auto sumf = ac + av + 1.0; |
| 329 |
static_assert(is_same<int, decltype(ac)>::value == true, ""); |
| 330 |
static_assert(is_same<int, decltype(av)>::value == true, ""); |
| 331 |
static_assert(is_same<int, decltype(sumi)>::value == true, ""); |
| 332 |
static_assert(is_same<int, decltype(sumf)>::value == false, ""); |
| 333 |
static_assert(is_same<int, decltype(add(c, v))>::value == true, ""); |
| 334 |
return (sumf > 0.0) ? sumi : add(c, v); |
| 335 |
} |
| 336 |
|
| 337 |
} |
| 338 |
|
| 339 |
namespace test_noexcept |
| 340 |
{ |
| 341 |
|
| 342 |
int f() { return 0; } |
| 343 |
int g() noexcept { return 0; } |
| 344 |
|
| 345 |
static_assert(noexcept(f()) == false, ""); |
| 346 |
static_assert(noexcept(g()) == true, ""); |
| 347 |
|
| 348 |
} |
| 349 |
|
| 350 |
namespace test_constexpr |
| 351 |
{ |
| 352 |
|
| 353 |
template < typename CharT > |
| 354 |
unsigned long constexpr |
| 355 |
strlen_c_r(const CharT *const s, const unsigned long acc) noexcept |
| 356 |
{ |
| 357 |
return *s ? strlen_c_r(s + 1, acc + 1) : acc; |
| 358 |
} |
| 359 |
|
| 360 |
template < typename CharT > |
| 361 |
unsigned long constexpr |
| 362 |
strlen_c(const CharT *const s) noexcept |
| 363 |
{ |
| 364 |
return strlen_c_r(s, 0UL); |
| 365 |
} |
| 366 |
|
| 367 |
static_assert(strlen_c("") == 0UL, ""); |
| 368 |
static_assert(strlen_c("1") == 1UL, ""); |
| 369 |
static_assert(strlen_c("example") == 7UL, ""); |
| 370 |
static_assert(strlen_c("another\0example") == 7UL, ""); |
| 371 |
|
| 372 |
} |
| 373 |
|
| 374 |
namespace test_rvalue_references |
| 375 |
{ |
| 376 |
|
| 377 |
template < int N > |
| 378 |
struct answer |
| 379 |
{ |
| 380 |
static constexpr int value = N; |
| 381 |
}; |
| 382 |
|
| 383 |
answer<1> f(int&) { return answer<1>(); } |
| 384 |
answer<2> f(const int&) { return answer<2>(); } |
| 385 |
answer<3> f(int&&) { return answer<3>(); } |
| 386 |
|
| 387 |
void |
| 388 |
test() |
| 389 |
{ |
| 390 |
int i = 0; |
| 391 |
const int c = 0; |
| 392 |
static_assert(decltype(f(i))::value == 1, ""); |
| 393 |
static_assert(decltype(f(c))::value == 2, ""); |
| 394 |
static_assert(decltype(f(0))::value == 3, ""); |
| 395 |
} |
| 396 |
|
| 397 |
} |
| 398 |
|
| 399 |
namespace test_uniform_initialization |
| 400 |
{ |
| 401 |
|
| 402 |
struct test |
| 403 |
{ |
| 404 |
static const int zero {}; |
| 405 |
static const int one {1}; |
| 406 |
}; |
| 407 |
|
| 408 |
static_assert(test::zero == 0, ""); |
| 409 |
static_assert(test::one == 1, ""); |
| 410 |
|
| 411 |
} |
| 412 |
|
| 413 |
namespace test_lambdas |
| 414 |
{ |
| 415 |
|
| 416 |
void |
| 417 |
test1() |
| 418 |
{ |
| 419 |
auto lambda1 = [](){}; |
| 420 |
auto lambda2 = lambda1; |
| 421 |
lambda1(); |
| 422 |
lambda2(); |
| 423 |
} |
| 424 |
|
| 425 |
int |
| 426 |
test2() |
| 427 |
{ |
| 428 |
auto a = [](int i, int j){ return i + j; }(1, 2); |
| 429 |
auto b = []() -> int { return '0'; }(); |
| 430 |
auto c = [=](){ return a + b; }(); |
| 431 |
auto d = [&](){ return c; }(); |
| 432 |
auto e = [a, &b](int x) mutable { |
| 433 |
const auto identity = [](int y){ return y; }; |
| 434 |
for (auto i = 0; i < a; ++i) |
| 435 |
a += b--; |
| 436 |
return x + identity(a + b); |
| 437 |
}(0); |
| 438 |
return a + b + c + d + e; |
| 439 |
} |
| 440 |
|
| 441 |
int |
| 442 |
test3() |
| 443 |
{ |
| 444 |
const auto nullary = [](){ return 0; }; |
| 445 |
const auto unary = [](int x){ return x; }; |
| 446 |
using nullary_t = decltype(nullary); |
| 447 |
using unary_t = decltype(unary); |
| 448 |
const auto higher1st = [](nullary_t f){ return f(); }; |
| 449 |
const auto higher2nd = [unary](nullary_t f1){ |
| 450 |
return [unary, f1](unary_t f2){ return f2(unary(f1())); }; |
| 451 |
}; |
| 452 |
return higher1st(nullary) + higher2nd(nullary)(unary); |
| 453 |
} |
| 454 |
|
| 455 |
} |
| 456 |
|
| 457 |
namespace test_variadic_templates |
| 458 |
{ |
| 459 |
|
| 460 |
template <int...> |
| 461 |
struct sum; |
| 462 |
|
| 463 |
template <int N0, int... N1toN> |
| 464 |
struct sum<N0, N1toN...> |
| 465 |
{ |
| 466 |
static constexpr auto value = N0 + sum<N1toN...>::value; |
| 467 |
}; |
| 468 |
|
| 469 |
template <> |
| 470 |
struct sum<> |
| 471 |
{ |
| 472 |
static constexpr auto value = 0; |
| 473 |
}; |
| 474 |
|
| 475 |
static_assert(sum<>::value == 0, ""); |
| 476 |
static_assert(sum<1>::value == 1, ""); |
| 477 |
static_assert(sum<23>::value == 23, ""); |
| 478 |
static_assert(sum<1, 2>::value == 3, ""); |
| 479 |
static_assert(sum<5, 5, 11>::value == 21, ""); |
| 480 |
static_assert(sum<2, 3, 5, 7, 11, 13>::value == 41, ""); |
| 481 |
|
| 482 |
} |
| 483 |
|
| 484 |
// http://stackoverflow.com/questions/13728184/template-aliases-and-sfinae |
| 485 |
// Clang 3.1 fails with headers of libstd++ 4.8.3 when using std::function |
| 486 |
// because of this. |
| 487 |
namespace test_template_alias_sfinae |
| 488 |
{ |
| 489 |
|
| 490 |
struct foo {}; |
| 491 |
|
| 492 |
template<typename T> |
| 493 |
using member = typename T::member_type; |
| 494 |
|
| 495 |
template<typename T> |
| 496 |
void func(...) {} |
| 497 |
|
| 498 |
template<typename T> |
| 499 |
void func(member<T>*) {} |
| 500 |
|
| 501 |
void test(); |
| 502 |
|
| 503 |
void test() { func<foo>(0); } |
| 504 |
|
| 505 |
} |
| 506 |
|
| 507 |
} // namespace cxx11 |
| 508 |
|
| 509 |
#endif // __cplusplus >= 201103L |
| 510 |
|
| 511 |
]]) |
| 512 |
|
| 513 |
|
| 514 |
dnl Tests for new features in C++14 |
| 515 |
|
| 516 |
m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_14], [[ |
| 517 |
|
| 518 |
// If the compiler admits that it is not ready for C++14, why torture it? |
| 519 |
// Hopefully, this will speed up the test. |
| 520 |
|
| 521 |
#ifndef __cplusplus |
| 522 |
|
| 523 |
#error "This is not a C++ compiler" |
| 524 |
|
| 525 |
#elif __cplusplus < 201402L && !defined _MSC_VER |
| 526 |
|
| 527 |
#error "This is not a C++14 compiler" |
| 528 |
|
| 529 |
#else |
| 530 |
|
| 531 |
namespace cxx14 |
| 532 |
{ |
| 533 |
|
| 534 |
namespace test_polymorphic_lambdas |
| 535 |
{ |
| 536 |
|
| 537 |
int |
| 538 |
test() |
| 539 |
{ |
| 540 |
const auto lambda = [](auto&&... args){ |
| 541 |
const auto istiny = [](auto x){ |
| 542 |
return (sizeof(x) == 1UL) ? 1 : 0; |
| 543 |
}; |
| 544 |
const int aretiny[] = { istiny(args)... }; |
| 545 |
return aretiny[0]; |
| 546 |
}; |
| 547 |
return lambda(1, 1L, 1.0f, '1'); |
| 548 |
} |
| 549 |
|
| 550 |
} |
| 551 |
|
| 552 |
namespace test_binary_literals |
| 553 |
{ |
| 554 |
|
| 555 |
constexpr auto ivii = 0b0000000000101010; |
| 556 |
static_assert(ivii == 42, "wrong value"); |
| 557 |
|
| 558 |
} |
| 559 |
|
| 560 |
namespace test_generalized_constexpr |
| 561 |
{ |
| 562 |
|
| 563 |
template < typename CharT > |
| 564 |
constexpr unsigned long |
| 565 |
strlen_c(const CharT *const s) noexcept |
| 566 |
{ |
| 567 |
auto length = 0UL; |
| 568 |
for (auto p = s; *p; ++p) |
| 569 |
++length; |
| 570 |
return length; |
| 571 |
} |
| 572 |
|
| 573 |
static_assert(strlen_c("") == 0UL, ""); |
| 574 |
static_assert(strlen_c("x") == 1UL, ""); |
| 575 |
static_assert(strlen_c("test") == 4UL, ""); |
| 576 |
static_assert(strlen_c("another\0test") == 7UL, ""); |
| 577 |
|
| 578 |
} |
| 579 |
|
| 580 |
namespace test_lambda_init_capture |
| 581 |
{ |
| 582 |
|
| 583 |
int |
| 584 |
test() |
| 585 |
{ |
| 586 |
auto x = 0; |
| 587 |
const auto lambda1 = [a = x](int b){ return a + b; }; |
| 588 |
const auto lambda2 = [a = lambda1(x)](){ return a; }; |
| 589 |
return lambda2(); |
| 590 |
} |
| 591 |
|
| 592 |
} |
| 593 |
|
| 594 |
namespace test_digit_separators |
| 595 |
{ |
| 596 |
|
| 597 |
constexpr auto ten_million = 100'000'000; |
| 598 |
static_assert(ten_million == 100000000, ""); |
| 599 |
|
| 600 |
} |
| 601 |
|
| 602 |
namespace test_return_type_deduction |
| 603 |
{ |
| 604 |
|
| 605 |
auto f(int& x) { return x; } |
| 606 |
decltype(auto) g(int& x) { return x; } |
| 607 |
|
| 608 |
template < typename T1, typename T2 > |
| 609 |
struct is_same |
| 610 |
{ |
| 611 |
static constexpr auto value = false; |
| 612 |
}; |
| 613 |
|
| 614 |
template < typename T > |
| 615 |
struct is_same<T, T> |
| 616 |
{ |
| 617 |
static constexpr auto value = true; |
| 618 |
}; |
| 619 |
|
| 620 |
int |
| 621 |
test() |
| 622 |
{ |
| 623 |
auto x = 0; |
| 624 |
static_assert(is_same<int, decltype(f(x))>::value, ""); |
| 625 |
static_assert(is_same<int&, decltype(g(x))>::value, ""); |
| 626 |
return x; |
| 627 |
} |
| 628 |
|
| 629 |
} |
| 630 |
|
| 631 |
} // namespace cxx14 |
| 632 |
|
| 633 |
#endif // __cplusplus >= 201402L |
| 634 |
|
| 635 |
]]) |
| 636 |
|
| 637 |
|
| 638 |
dnl Tests for new features in C++17 |
| 639 |
|
| 640 |
m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_17], [[ |
| 641 |
|
| 642 |
// If the compiler admits that it is not ready for C++17, why torture it? |
| 643 |
// Hopefully, this will speed up the test. |
| 644 |
|
| 645 |
#ifndef __cplusplus |
| 646 |
|
| 647 |
#error "This is not a C++ compiler" |
| 648 |
|
| 649 |
#elif (defined _MSVC_LANG ? _MSVC_LANG : __cplusplus) < 201703L |
| 650 |
|
| 651 |
#error "This is not a C++17 compiler" |
| 652 |
|
| 653 |
#else |
| 654 |
|
| 655 |
#include <initializer_list> |
| 656 |
#include <utility> |
| 657 |
#include <type_traits> |
| 658 |
|
| 659 |
namespace cxx17 |
| 660 |
{ |
| 661 |
|
| 662 |
namespace test_constexpr_lambdas |
| 663 |
{ |
| 664 |
|
| 665 |
constexpr int foo = [](){return 42;}(); |
| 666 |
|
| 667 |
} |
| 668 |
|
| 669 |
namespace test::nested_namespace::definitions |
| 670 |
{ |
| 671 |
|
| 672 |
} |
| 673 |
|
| 674 |
namespace test_fold_expression |
| 675 |
{ |
| 676 |
|
| 677 |
template<typename... Args> |
| 678 |
int multiply(Args... args) |
| 679 |
{ |
| 680 |
return (args * ... * 1); |
| 681 |
} |
| 682 |
|
| 683 |
template<typename... Args> |
| 684 |
bool all(Args... args) |
| 685 |
{ |
| 686 |
return (args && ...); |
| 687 |
} |
| 688 |
|
| 689 |
} |
| 690 |
|
| 691 |
namespace test_extended_static_assert |
| 692 |
{ |
| 693 |
|
| 694 |
static_assert (true); |
| 695 |
|
| 696 |
} |
| 697 |
|
| 698 |
namespace test_auto_brace_init_list |
| 699 |
{ |
| 700 |
|
| 701 |
auto foo = {5}; |
| 702 |
auto bar {5}; |
| 703 |
|
| 704 |
static_assert(std::is_same<std::initializer_list<int>, decltype(foo)>::value); |
| 705 |
static_assert(std::is_same<int, decltype(bar)>::value); |
| 706 |
} |
| 707 |
|
| 708 |
namespace test_typename_in_template_template_parameter |
| 709 |
{ |
| 710 |
|
| 711 |
template<template<typename> typename X> struct D; |
| 712 |
|
| 713 |
} |
| 714 |
|
| 715 |
namespace test_fallthrough_nodiscard_maybe_unused_attributes |
| 716 |
{ |
| 717 |
|
| 718 |
int f1() |
| 719 |
{ |
| 720 |
return 42; |
| 721 |
} |
| 722 |
|
| 723 |
[[nodiscard]] int f2() |
| 724 |
{ |
| 725 |
[[maybe_unused]] auto unused = f1(); |
| 726 |
|
| 727 |
switch (f1()) |
| 728 |
{ |
| 729 |
case 17: |
| 730 |
f1(); |
| 731 |
[[fallthrough]]; |
| 732 |
case 42: |
| 733 |
f1(); |
| 734 |
} |
| 735 |
return f1(); |
| 736 |
} |
| 737 |
|
| 738 |
} |
| 739 |
|
| 740 |
namespace test_extended_aggregate_initialization |
| 741 |
{ |
| 742 |
|
| 743 |
struct base1 |
| 744 |
{ |
| 745 |
int b1, b2 = 42; |
| 746 |
}; |
| 747 |
|
| 748 |
struct base2 |
| 749 |
{ |
| 750 |
base2() { |
| 751 |
b3 = 42; |
| 752 |
} |
| 753 |
int b3; |
| 754 |
}; |
| 755 |
|
| 756 |
struct derived : base1, base2 |
| 757 |
{ |
| 758 |
int d; |
| 759 |
}; |
| 760 |
|
| 761 |
derived d1 {{1, 2}, {}, 4}; // full initialization |
| 762 |
derived d2 {{}, {}, 4}; // value-initialized bases |
| 763 |
|
| 764 |
} |
| 765 |
|
| 766 |
namespace test_general_range_based_for_loop |
| 767 |
{ |
| 768 |
|
| 769 |
struct iter |
| 770 |
{ |
| 771 |
int i; |
| 772 |
|
| 773 |
int& operator* () |
| 774 |
{ |
| 775 |
return i; |
| 776 |
} |
| 777 |
|
| 778 |
const int& operator* () const |
| 779 |
{ |
| 780 |
return i; |
| 781 |
} |
| 782 |
|
| 783 |
iter& operator++() |
| 784 |
{ |
| 785 |
++i; |
| 786 |
return *this; |
| 787 |
} |
| 788 |
}; |
| 789 |
|
| 790 |
struct sentinel |
| 791 |
{ |
| 792 |
int i; |
| 793 |
}; |
| 794 |
|
| 795 |
bool operator== (const iter& i, const sentinel& s) |
| 796 |
{ |
| 797 |
return i.i == s.i; |
| 798 |
} |
| 799 |
|
| 800 |
bool operator!= (const iter& i, const sentinel& s) |
| 801 |
{ |
| 802 |
return !(i == s); |
| 803 |
} |
| 804 |
|
| 805 |
struct range |
| 806 |
{ |
| 807 |
iter begin() const |
| 808 |
{ |
| 809 |
return {0}; |
| 810 |
} |
| 811 |
|
| 812 |
sentinel end() const |
| 813 |
{ |
| 814 |
return {5}; |
| 815 |
} |
| 816 |
}; |
| 817 |
|
| 818 |
void f() |
| 819 |
{ |
| 820 |
range r {}; |
| 821 |
|
| 822 |
for (auto i : r) |
| 823 |
{ |
| 824 |
[[maybe_unused]] auto v = i; |
| 825 |
} |
| 826 |
} |
| 827 |
|
| 828 |
} |
| 829 |
|
| 830 |
namespace test_lambda_capture_asterisk_this_by_value |
| 831 |
{ |
| 832 |
|
| 833 |
struct t |
| 834 |
{ |
| 835 |
int i; |
| 836 |
int foo() |
| 837 |
{ |
| 838 |
return [*this]() |
| 839 |
{ |
| 840 |
return i; |
| 841 |
}(); |
| 842 |
} |
| 843 |
}; |
| 844 |
|
| 845 |
} |
| 846 |
|
| 847 |
namespace test_enum_class_construction |
| 848 |
{ |
| 849 |
|
| 850 |
enum class byte : unsigned char |
| 851 |
{}; |
| 852 |
|
| 853 |
byte foo {42}; |
| 854 |
|
| 855 |
} |
| 856 |
|
| 857 |
namespace test_constexpr_if |
| 858 |
{ |
| 859 |
|
| 860 |
template <bool cond> |
| 861 |
int f () |
| 862 |
{ |
| 863 |
if constexpr(cond) |
| 864 |
{ |
| 865 |
return 13; |
| 866 |
} |
| 867 |
else |
| 868 |
{ |
| 869 |
return 42; |
| 870 |
} |
| 871 |
} |
| 872 |
|
| 873 |
} |
| 874 |
|
| 875 |
namespace test_selection_statement_with_initializer |
| 876 |
{ |
| 877 |
|
| 878 |
int f() |
| 879 |
{ |
| 880 |
return 13; |
| 881 |
} |
| 882 |
|
| 883 |
int f2() |
| 884 |
{ |
| 885 |
if (auto i = f(); i > 0) |
| 886 |
{ |
| 887 |
return 3; |
| 888 |
} |
| 889 |
|
| 890 |
switch (auto i = f(); i + 4) |
| 891 |
{ |
| 892 |
case 17: |
| 893 |
return 2; |
| 894 |
|
| 895 |
default: |
| 896 |
return 1; |
| 897 |
} |
| 898 |
} |
| 899 |
|
| 900 |
} |
| 901 |
|
| 902 |
namespace test_template_argument_deduction_for_class_templates |
| 903 |
{ |
| 904 |
|
| 905 |
template <typename T1, typename T2> |
| 906 |
struct pair |
| 907 |
{ |
| 908 |
pair (T1 p1, T2 p2) |
| 909 |
: m1 {p1}, |
| 910 |
m2 {p2} |
| 911 |
{} |
| 912 |
|
| 913 |
T1 m1; |
| 914 |
T2 m2; |
| 915 |
}; |
| 916 |
|
| 917 |
void f() |
| 918 |
{ |
| 919 |
[[maybe_unused]] auto p = pair{13, 42u}; |
| 920 |
} |
| 921 |
|
| 922 |
} |
| 923 |
|
| 924 |
namespace test_non_type_auto_template_parameters |
| 925 |
{ |
| 926 |
|
| 927 |
template <auto n> |
| 928 |
struct B |
| 929 |
{}; |
| 930 |
|
| 931 |
B<5> b1; |
| 932 |
B<'a'> b2; |
| 933 |
|
| 934 |
} |
| 935 |
|
| 936 |
namespace test_structured_bindings |
| 937 |
{ |
| 938 |
|
| 939 |
int arr[2] = { 1, 2 }; |
| 940 |
std::pair<int, int> pr = { 1, 2 }; |
| 941 |
|
| 942 |
auto f1() -> int(&)[2] |
| 943 |
{ |
| 944 |
return arr; |
| 945 |
} |
| 946 |
|
| 947 |
auto f2() -> std::pair<int, int>& |
| 948 |
{ |
| 949 |
return pr; |
| 950 |
} |
| 951 |
|
| 952 |
struct S |
| 953 |
{ |
| 954 |
int x1 : 2; |
| 955 |
volatile double y1; |
| 956 |
}; |
| 957 |
|
| 958 |
S f3() |
| 959 |
{ |
| 960 |
return {}; |
| 961 |
} |
| 962 |
|
| 963 |
auto [ x1, y1 ] = f1(); |
| 964 |
auto& [ xr1, yr1 ] = f1(); |
| 965 |
auto [ x2, y2 ] = f2(); |
| 966 |
auto& [ xr2, yr2 ] = f2(); |
| 967 |
const auto [ x3, y3 ] = f3(); |
| 968 |
|
| 969 |
} |
| 970 |
|
| 971 |
namespace test_exception_spec_type_system |
| 972 |
{ |
| 973 |
|
| 974 |
struct Good {}; |
| 975 |
struct Bad {}; |
| 976 |
|
| 977 |
void g1() noexcept; |
| 978 |
void g2(); |
| 979 |
|
| 980 |
template<typename T> |
| 981 |
Bad |
| 982 |
f(T*, T*); |
| 983 |
|
| 984 |
template<typename T1, typename T2> |
| 985 |
Good |
| 986 |
f(T1*, T2*); |
| 987 |
|
| 988 |
static_assert (std::is_same_v<Good, decltype(f(g1, g2))>); |
| 989 |
|
| 990 |
} |
| 991 |
|
| 992 |
namespace test_inline_variables |
| 993 |
{ |
| 994 |
|
| 995 |
template<class T> void f(T) |
| 996 |
{} |
| 997 |
|
| 998 |
template<class T> inline T g(T) |
| 999 |
{ |
| 1000 |
return T{}; |
| 1001 |
} |
| 1002 |
|
| 1003 |
template<> inline void f<>(int) |
| 1004 |
{} |
| 1005 |
|
| 1006 |
template<> int g<>(int) |
| 1007 |
{ |
| 1008 |
return 5; |
| 1009 |
} |
| 1010 |
|
| 1011 |
} |
| 1012 |
|
| 1013 |
} // namespace cxx17 |
| 1014 |
|
| 1015 |
#endif // (defined _MSVC_LANG ? _MSVC_LANG : __cplusplus) < 201703L |
| 1016 |
|
| 1017 |
]]) |
| 1018 |
|
| 1019 |
|
| 1020 |
dnl Tests for new features in C++20 |
| 1021 |
|
| 1022 |
m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_20], [[ |
| 1023 |
|
| 1024 |
#ifndef __cplusplus |
| 1025 |
|
| 1026 |
#error "This is not a C++ compiler" |
| 1027 |
|
| 1028 |
#elif (defined _MSVC_LANG ? _MSVC_LANG : __cplusplus) < 202002L |
| 1029 |
|
| 1030 |
#error "This is not a C++20 compiler" |
| 1031 |
|
| 1032 |
#else |
| 1033 |
|
| 1034 |
#include <version> |
| 1035 |
|
| 1036 |
namespace cxx20 |
| 1037 |
{ |
| 1038 |
|
| 1039 |
// As C++20 supports feature test macros in the standard, there is no |
| 1040 |
// immediate need to actually test for feature availability on the |
| 1041 |
// Autoconf side. |
| 1042 |
|
| 1043 |
} // namespace cxx20 |
| 1044 |
|
| 1045 |
#endif // (defined _MSVC_LANG ? _MSVC_LANG : __cplusplus) < 202002L |
| 1046 |
|
| 1047 |
]]) |
| 1048 |
|
| 1049 |
|
| 1050 |
dnl Tests for new features in C++23 |
| 1051 |
|
| 1052 |
m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_23], [[ |
| 1053 |
|
| 1054 |
#ifndef __cplusplus |
| 1055 |
|
| 1056 |
#error "This is not a C++ compiler" |
| 1057 |
|
| 1058 |
#elif (defined _MSVC_LANG ? _MSVC_LANG : __cplusplus) < 202302L |
| 1059 |
|
| 1060 |
#error "This is not a C++23 compiler" |
| 1061 |
|
| 1062 |
#else |
| 1063 |
|
| 1064 |
#include <version> |
| 1065 |
|
| 1066 |
namespace cxx23 |
| 1067 |
{ |
| 1068 |
|
| 1069 |
// As C++23 supports feature test macros in the standard, there is no |
| 1070 |
// immediate need to actually test for feature availability on the |
| 1071 |
// Autoconf side. |
| 1072 |
|
| 1073 |
} // namespace cxx23 |
| 1074 |
|
| 1075 |
#endif // (defined _MSVC_LANG ? _MSVC_LANG : __cplusplus) < 202302L |
| 1076 |
|
| 1077 |
]]) |