Don't set CMAKE_CXX_STANDARD to 11 by default

Use the compiler default C++ dialect if it is C++11 or higher and only
explicitly request C++11 if the compiler can't compile a small test
using C++11 features by default.

This prevents from unnecessarily adding -std=c++11 to the compiler flags
under Unix, even with compilers using e.g. C++17 by default.
This commit is contained in:
Vadim Zeitlin 2024-01-04 03:30:00 +01:00
parent 5faa70503a
commit 3b06ab65ff
3 changed files with 19 additions and 5 deletions

View file

@ -10,6 +10,25 @@
if(DEFINED wxBUILD_CXX_STANDARD AND NOT wxBUILD_CXX_STANDARD STREQUAL COMPILER_DEFAULT)
set(CMAKE_CXX_STANDARD ${wxBUILD_CXX_STANDARD})
set(CMAKE_CXX_STANDARD_REQUIRED ON)
else()
# If the standard is not set explicitly, check whether we can use C++11
# without any special options.
include(CheckCXXSourceCompiles)
check_cxx_source_compiles("
#include <vector>
int main() {
std::vector<int> v{1,2,3};
for (auto& n : v)
--n;
return v[0];
}"
wxHAVE_CXX11)
if(NOT wxHAVE_CXX11)
# If not, request it explicitly and let CMake check if it's supported.
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
endif()
endif()
if(MSVC)

View file

@ -46,8 +46,6 @@ endif()
# support setting the C++ standard, present it an option to the user
if(DEFINED CMAKE_CXX_STANDARD)
set(wxCXX_STANDARD_DEFAULT ${CMAKE_CXX_STANDARD})
elseif(APPLE)
set(wxCXX_STANDARD_DEFAULT 11)
else()
set(wxCXX_STANDARD_DEFAULT COMPILER_DEFAULT)
endif()