1010#
1111# Check for baseline language coverage in the compiler for the specified
1212# 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) .
13+ # CXXCPP to enable support. VERSION may be '11', '14', '17', '20', or
14+ # '23' for the respective C++ standard version .
1515#
1616# The second argument, if specified, indicates whether you insist on an
1717# extended mode (e.g. -std=gnu++11) or a strict conformance mode (e.g.
3636# Copyright (c) 2016, 2018 Krzesimir Nowak <qdlacz@gmail.com>
3737# Copyright (c) 2019 Enji Cooper <yaneurabeya@gmail.com>
3838# Copyright (c) 2020 Jason Merrill <jason@redhat.com>
39+ # Copyright (c) 2021, 2024 Jörn Heusipp <osmanx@problemloesungsmaschine.de>
40+ # Copyright (c) 2015, 2022, 2023, 2024 Olly Betts
3941#
4042# Copying and distribution of this file, with or without modification, are
4143# permitted in any medium without royalty provided the copyright notice
4244# and this notice are preserved. This file is offered as-is, without any
4345# warranty.
4446
45- # serial 12
47+ # serial 25
4648
4749dnl This macro is based on the code from the AX_CXX_COMPILE_STDCXX_11 macro
4850dnl (serial version number 13).
@@ -51,6 +53,8 @@ AC_DEFUN([AX_CXX_COMPILE_STDCXX], [dnl
5153 m4_if ( [ $1 ] , [ 11] , [ ax_cxx_compile_alternatives="11 0x"] ,
5254 [ $1 ] , [ 14] , [ ax_cxx_compile_alternatives="14 1y"] ,
5355 [ $1 ] , [ 17] , [ ax_cxx_compile_alternatives="17 1z"] ,
56+ [ $1 ] , [ 20] , [ ax_cxx_compile_alternatives="20"] ,
57+ [ $1 ] , [ 23] , [ ax_cxx_compile_alternatives="23"] ,
5458 [ m4_fatal ( [ invalid first argument `$1 ' to AX_CXX_COMPILE_STDCXX ] ) ] ) dnl
5559 m4_if ( [ $2 ] , [ ] , [ ] ,
5660 [ $2 ] , [ ext] , [ ] ,
@@ -102,9 +106,18 @@ AC_DEFUN([AX_CXX_COMPILE_STDCXX], [dnl
102106 dnl HP's aCC needs +std=c++11 according to:
103107 dnl http://h21007.www2.hp.com/portal/download/files/unprot/aCxx/PDF_Release_Notes/769149-001.pdf
104108 dnl Cray's crayCC needs "-h std=c++11"
109+ dnl MSVC needs -std:c++NN for C++17 and later (default is C++14)
105110 for alternative in ${ax_cxx_compile_alternatives}; do
106- for switch in -std=c++${alternative} +std=c++${alternative} "-h std=c++${alternative}"; do
107- cachevar=AS_TR_SH ( [ ax_cv_cxx_compile_cxx$1 _$switch] )
111+ for switch in -std=c++${alternative} +std=c++${alternative} "-h std=c++${alternative}" MSVC; do
112+ if test x"$switch" = xMSVC; then
113+ dnl AS_TR_SH maps both `:` and `=` to `_` so -std:c++17 would collide
114+ dnl with -std=c++17. We suffix the cache variable name with _MSVC to
115+ dnl avoid this.
116+ switch=-std:c++${alternative}
117+ cachevar=AS_TR_SH ( [ ax_cv_cxx_compile_cxx$1 _${switch}_MSVC] )
118+ else
119+ cachevar=AS_TR_SH ( [ ax_cv_cxx_compile_cxx$1 _$switch] )
120+ fi
108121 AC_CACHE_CHECK ( whether $CXX supports C++$1 features with $switch ,
109122 $cachevar ,
110123 [ ac_save_CXX="$CXX"
@@ -148,23 +161,44 @@ AC_DEFUN([AX_CXX_COMPILE_STDCXX], [dnl
148161dnl Test body for checking C++11 support
149162
150163m4_define ( [ _AX_CXX_COMPILE_STDCXX_testbody_11] ,
151- _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
164+ [ _AX_CXX_COMPILE_STDCXX_testbody_new_in_11]
152165)
153166
154-
155167dnl Test body for checking C++14 support
156168
157169m4_define ( [ _AX_CXX_COMPILE_STDCXX_testbody_14] ,
158- _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
159- _AX_CXX_COMPILE_STDCXX_testbody_new_in_14
170+ [ _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
171+ _AX_CXX_COMPILE_STDCXX_testbody_new_in_14]
160172)
161173
174+ dnl Test body for checking C++17 support
175+
162176m4_define ( [ _AX_CXX_COMPILE_STDCXX_testbody_17] ,
163- _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
164- _AX_CXX_COMPILE_STDCXX_testbody_new_in_14
165- _AX_CXX_COMPILE_STDCXX_testbody_new_in_17
177+ [ _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
178+ _AX_CXX_COMPILE_STDCXX_testbody_new_in_14
179+ _AX_CXX_COMPILE_STDCXX_testbody_new_in_17]
180+ )
181+
182+ dnl Test body for checking C++20 support
183+
184+ m4_define ( [ _AX_CXX_COMPILE_STDCXX_testbody_20] ,
185+ [ _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
186+ _AX_CXX_COMPILE_STDCXX_testbody_new_in_14
187+ _AX_CXX_COMPILE_STDCXX_testbody_new_in_17
188+ _AX_CXX_COMPILE_STDCXX_testbody_new_in_20]
166189)
167190
191+ dnl Test body for checking C++23 support
192+
193+ m4_define ( [ _AX_CXX_COMPILE_STDCXX_testbody_23] ,
194+ [ _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
195+ _AX_CXX_COMPILE_STDCXX_testbody_new_in_14
196+ _AX_CXX_COMPILE_STDCXX_testbody_new_in_17
197+ _AX_CXX_COMPILE_STDCXX_testbody_new_in_20
198+ _AX_CXX_COMPILE_STDCXX_testbody_new_in_23]
199+ )
200+
201+
168202dnl Tests for new features in C++11
169203
170204m4_define ( [ _AX_CXX_COMPILE_STDCXX_testbody_new_in_11] , [ [
@@ -176,7 +210,21 @@ m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_11], [[
176210
177211#error "This is not a C++ compiler"
178212
179- #elif __cplusplus < 201103L
213+ // MSVC always sets __cplusplus to 199711L in older versions; newer versions
214+ // only set it correctly if /Zc:__cplusplus is specified as well as a
215+ // /std:c++NN switch:
216+ //
217+ // https://devblogs.microsoft.com/cppblog/msvc-now-correctly-reports-__cplusplus/
218+ //
219+ // The value __cplusplus ought to have is available in _MSVC_LANG since
220+ // Visual Studio 2015 Update 3:
221+ //
222+ // https://learn.microsoft.com/en-us/cpp/preprocessor/predefined-macros
223+ //
224+ // This was also the first MSVC version to support C++14 so we can't use the
225+ // value of either __cplusplus or _MSVC_LANG to quickly rule out MSVC having
226+ // C++11 or C++14 support, but we can check _MSVC_LANG for C++17 and later.
227+ #elif __cplusplus < 201103L && !defined _MSC_VER
180228
181229#error "This is not a C++11 compiler"
182230
@@ -467,7 +515,7 @@ m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_14], [[
467515
468516#error "This is not a C++ compiler"
469517
470- #elif __cplusplus < 201402L
518+ #elif __cplusplus < 201402L && !defined _MSC_VER
471519
472520#error "This is not a C++14 compiler"
473521
@@ -591,7 +639,7 @@ m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_17], [[
591639
592640#error "This is not a C++ compiler"
593641
594- #elif __cplusplus < 201703L
642+ #elif (defined _MSVC_LANG ? _MSVC_LANG : __cplusplus) < 201703L
595643
596644#error "This is not a C++17 compiler"
597645
@@ -957,8 +1005,66 @@ namespace cxx17
9571005
9581006} // namespace cxx17
9591007
960- #endif // __cplusplus < 201703L
1008+ #endif // (defined _MSVC_LANG ? _MSVC_LANG : __cplusplus) < 201703L
1009+
1010+ ] ] )
1011+
1012+
1013+ dnl Tests for new features in C++20
1014+
1015+ m4_define ( [ _AX_CXX_COMPILE_STDCXX_testbody_new_in_20] , [ [
1016+
1017+ #ifndef __cplusplus
1018+
1019+ #error "This is not a C++ compiler"
1020+
1021+ #elif (defined _MSVC_LANG ? _MSVC_LANG : __cplusplus) < 202002L
1022+
1023+ #error "This is not a C++20 compiler"
1024+
1025+ #else
1026+
1027+ #include <version>
1028+
1029+ namespace cxx20
1030+ {
1031+
1032+ // As C++20 supports feature test macros in the standard, there is no
1033+ // immediate need to actually test for feature availability on the
1034+ // Autoconf side.
1035+
1036+ } // namespace cxx20
1037+
1038+ #endif // (defined _MSVC_LANG ? _MSVC_LANG : __cplusplus) < 202002L
9611039
9621040] ] )
9631041
9641042
1043+ dnl Tests for new features in C++23
1044+
1045+ m4_define ( [ _AX_CXX_COMPILE_STDCXX_testbody_new_in_23] , [ [
1046+
1047+ #ifndef __cplusplus
1048+
1049+ #error "This is not a C++ compiler"
1050+
1051+ #elif (defined _MSVC_LANG ? _MSVC_LANG : __cplusplus) < 202302L
1052+
1053+ #error "This is not a C++23 compiler"
1054+
1055+ #else
1056+
1057+ #include <version>
1058+
1059+ namespace cxx23
1060+ {
1061+
1062+ // As C++23 supports feature test macros in the standard, there is no
1063+ // immediate need to actually test for feature availability on the
1064+ // Autoconf side.
1065+
1066+ } // namespace cxx23
1067+
1068+ #endif // (defined _MSVC_LANG ? _MSVC_LANG : __cplusplus) < 202302L
1069+
1070+ ] ] )
0 commit comments