DCMTK: Modern C++ Support in DCMTK

DCMTK Version 3.6.9 OFFIS DICOM Toolkit

Loading...
Searching...
No Matches
Modern C++ Support in DCMTK

Per default, DCMTK is compiled with C++11 support enabled (via `CMAKE_CXX_STANDARD` set to 11). This usually increases performance since some operations can be performed in a more efficient manner, e.g. utilizing move semantics.

Later C++ standards can be enabled using the `CMAKE_CXX_STANDARD` setting. DCMTK's INSTALL file describes the available C++ standard versions that are known to work for the given DCMTK version.

DCMTK can still be compiled using a C++98 compiler. However, C++98 support will be be dropped in the future.

# C++11 STL Classes

While some features like move semantics are automatically enabled in legacy code when compiling with C++11 enabled, modern C++ STL classes like std::unique_ptr must be used explicitly.

Since DCMTK does not require a C++11-aware compiler (yet), the toolkit comes with some wrapper classes for some of these STL features. Each wrapper class either directly maps to the respective C++11 feature (if made available, see below) or otherwise implements a C++98 fallback solution. For example, the following C++11 STL classes are implemented that way:

- std::unique_ptr (OFunique_ptr in DCMTK)
- std::shared_ptr (OFshared_ptr in DCMTK)
- std::numeric_limits (OFnumeric_limits in DCMTK)
- std::tuple (OFtuple in DCMTK)
- std::error_code (OFerror_code in DCMTK)

If you want to ensure that DCMTK uses the STL versions of these classes, there is an additional flag (besides enabling the related C++ version) to be set during CMake configuration:

Enabling the flag `DCMTK_ENABLE_STL` will ensure that whenever possible (i.e. supported correctly by the compiler) the STL version of a class is preferred over the fallback implementation.

There are several other flags in order to fine-tune this behavior:

- DCMTK_ENABLE_STL_MEMORY: Can be used to enable std::unique_ptr and related classes.
- DCMTK_ENABLE_STL_MAP: Can be used to enable stl::map.
- DCMTK_ENABLE_STL_...: There are further flags for specific STL features. They can be easily identified by their prefix.

Per default, those flags are set to "INFERRED", meaning that they take over the setting of `DCMTK_ENABLE_STL`. However, if you just want to enable or disable certain features (i.e. deviate from `DCMTK_ENABLE_STL`), you can set each of them explicitly to "ON" or "OFF".

# Using DCMTK with C++11

By compiling with C++11 or later and enabling the STL, you gain the following advantages:

- DCMTK can use available C++11 features instead of its only legacy implementations. These are probably more efficient and complete.
- The enabling of C++11 becomes part of DCMTK's configuration, which allows DCMTK to assist you when checking for API/ABI (in-)compatibility between C++11 and non C++11 builds.

Trying to include a DCMTK build with C++11-enabled from a non C++11 environment will give you the following error message:

    DCMTK was configured to use C++11 features, but your compiler does not or was
    not configured to provide them.

To reduce DCMTK's implementation complexity, C++11 shall only be used if your compiler conforms closely to the C++11 standard. As far as we know, this is currently true for the following compilers:

- GNU C++ Compiler (g++) Version 4.8.1 or later.
- Clang (clang++) Version 3.3 or later.
- According to Microsoft documentation, Visual Studio 2019 Version 16.8 (or higher) with Windows SDK 10.0.20348.0 Version 2104 (or higher). DCMTK already allows enabling C++11 for VS 2017 which reportedly also works well.

When compiling with C++11 or later, CMake will then perform basic checks to ensure the compiler is configured correctly and conforms closely enough to all modern C++ standards up to the selected standard version.

Below is a CMake output when (successfully) setting `CMAKE_CXX_STANDARD` to 14:

    ...
    -- Checking whether the compiler supports C++11
    -- Checking whether the compiler supports C++11 -- yes
    -- Checking whether the compiler supports C++14
    -- Checking whether the compiler supports C++14 -- yes
    ...

## GNU Autoconf

Our GNU Autoconf setup understands the switches `--enable-cxx11`, and `--disable-cxx11` that may be used to configure DCMTK to use C++11 features. If you run

    configure --enable-cxx11

our Autoconf script will try to determine the required compiler flags for enabling C++11 features (if any) and perform configuration tests to ensure the compiler conforms closely enough to the C++11 standard. If everything works it will give you an output similar to this one:

    checking whether to enable C++11 support...
    checking whether c++ supports C++11 features by default... no
    checking whether c++ supports C++11 features with -std=c++11... yes
    checking whether to enable C++11 support... yes

Otherwise, it will still disable C++11 features with an output like this:

    checking whether to enable C++11 support...
    checking whether c++ supports C++11 features by default... no
    checking whether c++ supports C++11 features with -std=c++11... no
    checking whether to enable C++11 support... no

There are further switches that enable STL features, comparable to the CMake configuration described above:

    --enable-stl            use C++ STL
    --disable-stl           do not use C++ STL (default)
    --enable-stl-vector     use C++ STL vector
    --disable-stl-vector    do not use C++ STL vector
    --enable-stl-algorithm  use C++ STL algorithm
    --disable-stl-algorithm do not use C++ STL algorithm
    ....

Support for setting C++ versions later than C++11 is not yet available in DCMTK's Autoconf settings.

Generated on Wed Apr 30 2025 for DCMTK Version 3.6.9 by Doxygen 1.13.2