From 3cbbaa64dda0fd18b4b18456ba78a686411005e4 Mon Sep 17 00:00:00 2001
From: Davis King
Date: Sat, 3 Oct 2026 17:02:29 -0400
Subject: [PATCH] Let's start using C++17
---
.github/workflows/build_cpp.yml | 2 +-
README.md | 6 ++---
dlib/CMakeLists.txt | 7 ++++--
dlib/algs.h | 16 ++++++--------
dlib/java/cmake_swig_jni | 2 +-
dlib/serialize.h | 2 +-
.../WINDOWS_build_and_run_all_unit_tests.bat | 12 +++++-----
dlib/test/constexpr_if.cpp | 2 +-
dlib/test/makefile | 2 +-
docs/docs/compile.xml | 18 +++++++--------
docs/docs/containers.xml | 2 +-
docs/docs/faq.xml | 22 ++++---------------
docs/docs/metaprogramming.xml | 2 +-
docs/docs/release_notes.xml | 13 ++++++++++-
setup.py | 2 +-
15 files changed, 54 insertions(+), 56 deletions(-)
diff --git a/.github/workflows/build_cpp.yml b/.github/workflows/build_cpp.yml
index 226d24d4bf..fb996a83fa 100644
--- a/.github/workflows/build_cpp.yml
+++ b/.github/workflows/build_cpp.yml
@@ -105,7 +105,7 @@ jobs:
sudo apt install gcc-11 g++-11
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 110 --slave /usr/bin/g++ g++ /usr/bin/g++-11 --slave /usr/bin/gcov gcov /usr/bin/gcov-11
- name: Configure
- run: cmake ${{ github.workspace }}/dlib/test -B build
+ run: cmake ${{ github.workspace }}/dlib/test -B build -DCMAKE_CXX_STANDARD=17 -DCMAKE_CXX_EXTENSIONS=OFF
- name: Build just tests
run: cmake --build build --config Release --target dtest --parallel 4
- name: Test
diff --git a/README.md b/README.md
index 51b2aeeecd..ff32038173 100644
--- a/README.md
+++ b/README.md
@@ -6,7 +6,7 @@ Dlib is a modern C++ toolkit containing machine learning algorithms and tools fo
## Compiling dlib C++ example programs
-Go into the examples folder and type:
+You need a C++17 compiler and CMake. Go into the examples folder and type:
```bash
mkdir build; cd build; cmake .. ; cmake --build .
@@ -21,9 +21,9 @@ mkdir build; cd build; cmake .. -DUSE_AVX_INSTRUCTIONS=1; cmake --build .
Doing so will make some things run faster.
-Finally, Visual Studio users should usually do everything in 64bit mode. By default Visual Studio is 32bit, both in its outputs and its own execution, so you have to explicitly tell it to use 64bits. Since it's not the 1990s anymore you probably want to use 64bits. Do that with a cmake invocation like this:
+Visual Studio users can explicitly select a 64bit target and compiler with a CMake invocation like this:
```bash
-cmake .. -G "Visual Studio 14 2015 Win64" -T host=x64
+cmake .. -G "Visual Studio 17 2022" -A x64 -T host=x64
```
## Compiling your own C++ programs that use dlib
diff --git a/dlib/CMakeLists.txt b/dlib/CMakeLists.txt
index 0600bf870d..289d4793cf 100644
--- a/dlib/CMakeLists.txt
+++ b/dlib/CMakeLists.txt
@@ -806,10 +806,13 @@ if (NOT TARGET dlib)
add_library(dlib_all_source_cpp STATIC all/source.cpp)
target_link_libraries(dlib_all_source_cpp dlib)
target_compile_options(dlib_all_source_cpp PUBLIC ${active_preprocessor_switches})
- target_compile_features(dlib_all_source_cpp PUBLIC cxx_std_14)
+ target_compile_features(dlib_all_source_cpp PUBLIC cxx_std_17)
endif()
- target_compile_features(dlib PUBLIC cxx_std_14)
+ target_compile_features(dlib PUBLIC cxx_std_17)
+ if (DLIB_USE_CUDA)
+ target_compile_features(dlib PRIVATE cuda_std_17)
+ endif()
# Build-tree clients share dlib's compiler, so forward the options detected
# above to them. Do not put those options in the installed target, where
# dlibConfig.cmake recreates them for the consuming compiler's frontend.
diff --git a/dlib/algs.h b/dlib/algs.h
index faa5ca1fe8..d6b0de9809 100644
--- a/dlib/algs.h
+++ b/dlib/algs.h
@@ -10,12 +10,14 @@
// this file contains miscellaneous stuff
-// Give people who forget the -std=c++14 option a reminder
-#if (defined(__GNUC__) && ((__GNUC__ >= 5 && __GNUC_MINOR__ >= 0) || (__GNUC__ > 5))) || \
- (defined(__clang__) && ((__clang_major__ >= 3 && __clang_minor__ >= 4) || (__clang_major__ >= 3)))
- #if __cplusplus < 201402L
- #error "Dlib requires C++14 support. Give your compiler the -std=c++14 option to enable it."
+// Give people who forget to enable C++17 a reminder.
+#if defined(_MSC_VER)
+ // MSVC only updates __cplusplus when /Zc:__cplusplus is enabled.
+ #if !defined(_MSVC_LANG) || _MSVC_LANG < 201703L
+ #error "Dlib requires C++17 support. Give your compiler the /std:c++17 option to enable it."
#endif
+#elif __cplusplus < 201703L
+ #error "Dlib requires C++17 support. Give your compiler the -std=c++17 option to enable it."
#endif
#if defined __NVCC__
@@ -32,10 +34,6 @@
#ifdef _MSC_VER
-#if _MSC_VER < 1900
-#error "dlib versions newer than v19.1 use C++11 and therefore require Visual Studio 2015 or newer."
-#endif
-
// Disable the following warnings for Visual Studio
// this is to disable the "'this' : used in base member initializer list"
diff --git a/dlib/java/cmake_swig_jni b/dlib/java/cmake_swig_jni
index 48f960f41f..a13530ad18 100644
--- a/dlib/java/cmake_swig_jni
+++ b/dlib/java/cmake_swig_jni
@@ -178,7 +178,7 @@ FILE(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/${binary_dir_name}")
set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/swig.i PROPERTIES CPLUSPLUS ON)
swig_add_module(${output_library_name} java ${CMAKE_CURRENT_BINARY_DIR}/swig.i ${source_files})
-target_compile_features(${output_library_name} PUBLIC cxx_std_14)
+target_compile_features(${output_library_name} PUBLIC cxx_std_17)
include_directories(${JNI_INCLUDE_DIRS})
swig_link_libraries(${output_library_name} ${additional_link_libraries})
diff --git a/dlib/serialize.h b/dlib/serialize.h
index ed8dbd8033..f056deb254 100644
--- a/dlib/serialize.h
+++ b/dlib/serialize.h
@@ -2231,7 +2231,7 @@ namespace dlib
//when deserializing unique_ptr, this is fresh state, so reset the pointers, even if item is non-empty
bool is_non_empty;
deserialize(is_non_empty, in);
- item.reset(is_non_empty ? new T() : nullptr); //can't use make_unique since dlib does not use C++14 as a minimum requirement.
+ item.reset(is_non_empty ? new T() : nullptr);
if (is_non_empty)
deserialize(*item, in);
diff --git a/dlib/test/WINDOWS_build_and_run_all_unit_tests.bat b/dlib/test/WINDOWS_build_and_run_all_unit_tests.bat
index 333ca1582c..6bba75b78e 100644
--- a/dlib/test/WINDOWS_build_and_run_all_unit_tests.bat
+++ b/dlib/test/WINDOWS_build_and_run_all_unit_tests.bat
@@ -9,18 +9,18 @@ echo testing python >> test_log.txt
rmdir /S /Q build_python
mkdir build_python
cd build_python
-cmake -G "Visual Studio 14 2015 Win64" -T host=x64 ../../../tools/python
+cmake -G "Visual Studio 17 2022" -A x64 -T host=x64 ../../../tools/python
cmake --build . --config Release || exit /B
ping 127.0.0.1 -n 5 -w 1000 > null
cd ..
-echo testing vc2015 >> test_log.txt
-rmdir /S /Q build_vc2015_64
-mkdir build_vc2015_64
-cd build_vc2015_64
-cmake -G "Visual Studio 14 2015 Win64" -T host=x64 ..
+echo testing vc2022 >> test_log.txt
+rmdir /S /Q build_vc2022_64
+mkdir build_vc2022_64
+cd build_vc2022_64
+cmake -G "Visual Studio 17 2022" -A x64 -T host=x64 ..
cmake --build . --config Release || exit /B
ping 127.0.0.1 -n 5 -w 1000 > null
cmake --build . --config Debug || exit /B
diff --git a/dlib/test/constexpr_if.cpp b/dlib/test/constexpr_if.cpp
index 31f5fd252a..aba24f252d 100644
--- a/dlib/test/constexpr_if.cpp
+++ b/dlib/test/constexpr_if.cpp
@@ -192,7 +192,7 @@ namespace
public:
constexpr_if_test (
) : tester ("test_constexpr_if",
- "Runs tests on the C++14 approximation of C++17 if constexpr() statements but better.")
+ "Runs tests on dlib's compile time dispatch utilities.")
{}
void perform_test (
diff --git a/dlib/test/makefile b/dlib/test/makefile
index 3ed1b9b469..550e2f61c9 100644
--- a/dlib/test/makefile
+++ b/dlib/test/makefile
@@ -6,7 +6,7 @@ TARGET = dtest
# these are the compile time flags passed to gcc
CXXFLAGS ?= -ggdb -Wall
-CPPFLAGS ?= -std=c++14 -DDEBUG -DDLIB_NO_GUI_SUPPORT -I../..
+CPPFLAGS ?= -std=c++17 -DDEBUG -DDLIB_NO_GUI_SUPPORT -I../..
# These are the link time flags passed to gcc
LFLAGS = -lpthread -lnsl
diff --git a/docs/docs/compile.xml b/docs/docs/compile.xml
index d0ef6fbc2e..43812060d7 100644
--- a/docs/docs/compile.xml
+++ b/docs/docs/compile.xml
@@ -13,7 +13,7 @@
from github, go to the base folder of the dlib repository,
and run python setup.py install. Once either of these commands
finishes running you are ready to use dlib from Python. Note
- that you need to have CMake and a working C++ compiler
+ that you need to have CMake and a working C++17 compiler
installed for this to work.
@@ -53,7 +53,7 @@ cd build
cmake ..
cmake --build . --config Release
-Note that you need to have a C++14 compiler installed on your system. There are free C++14 compilers
+Note that you need to have a C++17 compiler installed on your system. There are free C++17 compilers
for most operating systems. For example, Visual Studio is free on Windows and GCC is free and
works well on Mac OS X and Linux systems. If you have multiple compilers/IDEs installed then you can
tell CMake which one you want it to use via the -G option.
@@ -65,10 +65,9 @@ tell CMake which one you want it to use via the -G option.
makes it run much faster (also see this FAQ).
- Finally, note that when using Visual Studio, CMake will by default generate a 32bit executable.
- This means the programs you compile will only be able to use 2GB of RAM. To avoid this, you need
- to tell CMake to generate a 64bit executable. You do this by using a command like
- cmake -G "Visual Studio 14 2015 Win64" -T host=x64 .. instead of cmake ..
+ When using Visual Studio, select a 64bit target to avoid the memory limits of 32bit executables.
+ You can do this by using a command like
+ cmake -G "Visual Studio 17 2022" -A x64 -T host=x64 .. instead of cmake ..
You can see the list of valid arguments to -G by running cmake with no options. Note also the -T host=x64
option, which tells Visual Studio to let the compiler use more than 2GB of RAM. That is important if you don't want the compiler to
crash from running out of RAM in some situations.
@@ -119,7 +118,7 @@ tell CMake which one you want it to use via the -G option.
Compiling on Linux From Command Line
From within the examples folder, you can compile nearly all of the examples with a single command like so:
-g++ -std=c++14 -O3 -I.. ../dlib/all/source.cpp -lpthread -lX11 example_program_name.cpp
+g++ -std=c++17 -O3 -I.. ../dlib/all/source.cpp -lpthread -lX11 example_program_name.cpp
On non-Linux systems like Solaris, you might have to link to other libraries. For example, I have seen systems
@@ -136,9 +135,10 @@ sudo apt-get install libx11-dev
(via the -l option) to the following libraries: gdi32, comctl32, user32, winmm, ws2_32, or imm32.
- Compiling on Windows Using Visual Studio 2015 or Newer
+ Compiling on Windows Using Visual Studio
- All you need to do is create an empty console project. Then add dlib/all/source.cpp to it and add the
+ Create an empty console project and enable C++17 with /std:c++17.
+ Then add dlib/all/source.cpp to it and add the
folder containing the dlib folder to the #include search path. Then you can compile any example program
by adding it to your project.
diff --git a/docs/docs/containers.xml b/docs/docs/containers.xml
index 95b3e2a454..d710a4a97d 100644
--- a/docs/docs/containers.xml
+++ b/docs/docs/containers.xml
@@ -739,7 +739,7 @@
dlib/optional.h
This is a standard's compliant backport of std::optional
- that works with C++14. It includes C++23 monadic interfaces
+ that works with C++17. It includes C++23 monadic interfaces
diff --git a/docs/docs/faq.xml b/docs/docs/faq.xml
index 43cd20fed1..16d9e39e62 100644
--- a/docs/docs/faq.xml
+++ b/docs/docs/faq.xml
@@ -128,8 +128,8 @@
- First, note that you need a version of Visual Studio with decent
- C++11 support. This means you need Visual Studio 2015 or newer.
+ First, note that you need a version of Visual Studio with C++17 support.
+ Enable it with /std:c++17, or use CMake to select it automatically.
There are instructions on the How to Compile page.
If you do not understand the instructions in the "Compiling on Windows Using Visual Studio" section
@@ -554,22 +554,8 @@ cross_validate_trainer_threaded(trainer,
- You can, but you need to use Visual Studio 2015 Update 3 or newer since prior versions
- had bad C++11 support. To make this as confusing as possible,
- Microsoft has released multiple different versions of "Visual Studio
- 2015 Update 3". As of October 2016, the version available from the
- Microsoft web page has good enough C++11 support to compile the DNN
- tools in dlib. So make sure you have a version no older than October
- 2016.
-
- To make this even more complicated, Visual Studio 2017 had
- regressions in its C++11 support. So all versions of Visual Studio
- 2017 prior to December 2017 would just hang if you tried to compile
- the DNN examples. Happily, the newest versions of Visual Studio
- 2017 appear to have good C++11 support and will compile the DNN
- codes without any issue. So make sure your Visual Studio is
- fully updated.
-
+ You can, but you need a version of Visual Studio with C++17 support.
+ Keep Visual Studio fully updated and use CMake to enable the required compiler options.
Finally, it should be noted that you should give the -T host=x64
cmake option when generating a Visual Studio project. If you don't
diff --git a/docs/docs/metaprogramming.xml b/docs/docs/metaprogramming.xml
index b455a90ec4..2c09db965e 100644
--- a/docs/docs/metaprogramming.xml
+++ b/docs/docs/metaprogramming.xml
@@ -1002,7 +1002,7 @@ or you can omit the error message and call it like:
dlib/constexpr_if.h
dlib/constexpr_if.h
- This function allows you to do compile time dispatch similar to what you can do with constexpr if in C++17, but only requires a C++14 compiler.
+ This function allows you to do compile time dispatch similar to what you can do with if constexpr in C++17.
diff --git a/docs/docs/release_notes.xml b/docs/docs/release_notes.xml
index c166eb200e..19549210ee 100644
--- a/docs/docs/release_notes.xml
+++ b/docs/docs/release_notes.xml
@@ -11,6 +11,17 @@
+New Features and Improvements:
+
+Non-Backwards Compatible Changes:
+ - Dlib now requires C++17.
+
+Bug fixes:
+
+
+
+
+
New Features and Improvements:
- Machine Learning
- Added auto_train_multiclass_svm_linear_classifier(), a tool that trains a multiclass
@@ -69,7 +80,7 @@ Bug fixes:
- Fix pixel saturation in interpolate_quadratic (PR #2806)
- Fix convolution backward filter cache issue when using cudnn 7 and above (PR #2828)
- Fix a bug in the gradient computation code for the layer_normalize layer.
-
+
diff --git a/setup.py b/setup.py
index 519c5d415f..0f05ab1909 100644
--- a/setup.py
+++ b/setup.py
@@ -18,7 +18,7 @@
--no USE_AVX_INSTRUCTIONS: will set -DUSE_AVX_INSTRUCTIONS=no
Additional options:
--compiler-flags: pass flags onto the compiler, e.g. --compiler-flags "-Os -Wall" passes -Os -Wall onto GCC.
- -G: Set the CMake generator. E.g. -G "Visual Studio 14 2015"
+ -G: Set the CMake generator. E.g. -G "Visual Studio 17 2022"
--clean: delete any previous build folders and rebuild. You should do this if you change any build options
by setting --compiler-flags or --no since the last time you ran a build. This will
ensure the changes take effect.