Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cpp20-overview.qdoc
Go to the documentation of this file.
1// Copyright (C) 2024 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5 \page cpp20-overview.html
6 \title C++20 Overview
7 \brief An overview of C++20 features available in Qt.
8
9 Qt 6 officially requires C++17, however we are constantly working on
10 supporting new C++ language features to make Qt faster and safer, and
11 provide a better experience for our users.
12
13 This page gives a brief overview of C++20 features available in Qt.
14
15 \section1 WebEngine and Module-Specific Requirements
16
17 While Qt 6 only requires a C++17-compatible compiler in general,
18 the \l{Qt WebEngine} module requires a C++20-compatible compiler
19 due to its dependencies (such as Chromium).
20
21 If you are building Qt WebEngine, ensure that your compiler fully supports C++20.
22
23 See \l{qtwebengine-platform-notes.html}{Qt WebEngine Platform Notes} for detailed requirements.
24
25 \section1 Support for \c{std::chrono}
26
27 Various classes related to date and time have support for \l
28 {https://en.cppreference.com/w/cpp/chrono} {std::chrono} features from older
29 versions of C++. With the arrival of C++20, \c{std::chrono} has added new \l
30 {https://en.cppreference.com//w/cpp/chrono#Calendar} {calendar types}, such
31 as \l {https://en.cppreference.com/w/cpp/chrono/year_month_day}
32 {year_month_day}, plus \l
33 {https://en.cppreference.com//w/cpp/chrono#Time_point} {date and time
34 representations}, such as the \l
35 {https://en.cppreference.com/w/cpp/chrono/system_clock} {system_clock} and
36 its \l {https://en.cppreference.com/w/cpp/chrono/time_point} {time_point}
37 types. It also now provides access to the IANA database of \l
38 {https://en.cppreference.com/w/cpp/chrono#Time_zone} {timezone-related
39 information}. When Qt is built with C++20, it can now make use of these
40 additions.
41
42 QDate and QDateTime now support conversion to and from the various calendar,
43 date and date-time types, along with addition of \l
44 {https://en.cppreference.com/w/cpp/chrono/duration} {duration
45 types}. QTimeZone now supports construction from a \l
46 {https://en.cppreference.com/w/cpp/chrono/time_zone} {time_zone}.
47
48 QTimeZone can also, when built with feature \c timezone_tzdb enabled, use
49 the C++20 \l {https://en.cppreference.com/w/cpp/chrono/tzdb} {tzdb}
50 facilities in place of platform-specific backends to access timezone
51 information. This remains experimental in Qt 6.9 as some standard library
52 implementations do not faithfully represent the IANA data for timezones.
53
54 \section1 QSpan - a Qt version of \c {std::span}
55
56 The \l {https://en.cppreference.com/w/cpp/container/span}{std::span} class
57 template is an addition to the standard library that provides a uniform
58 way to access a contiguous portion of any contiguous container.
59
60 Using \c {std::span} in the public APIs of Qt could provide significant
61 advantages. However, Qt 6 only requires C++17, meaning it cannot have C++20
62 types in the API and ABI.
63
64 Qt 6.7 introduced \l QSpan - a Qt version of \c {std::span}. The API of the
65 class is compatible with the std version. \l QSpan and \c {std::span} can be
66 implicitly converted into each other. However, there are some differences
67 between the classes. See the corresponding section in the
68 \l {span-STL} {QSpan class documentation} for more details.
69
70 \section1 Three-way comparison operator
71
72 C++20 introduced \c {operator<=>()}, also known as the
73 \l {https://en.cppreference.com/w/cpp/language/operator_comparison#Three-way_comparison}
74 {three-way comparison operator}, and three ordering types to represent
75 the results of the comparison:
76
77 \list
78 \li \l {https://en.cppreference.com/w/cpp/utility/compare/strong_ordering}
79 {std::strong_ordering};
80 \li \l {https://en.cppreference.com/w/cpp/utility/compare/weak_ordering}
81 {std::weak_ordering};
82 \li \l {https://en.cppreference.com/w/cpp/utility/compare/partial_ordering}
83 {std::partial_ordering}.
84 \endlist
85
86 In Qt 6.8, many of the QtCore classes got support for \c {operator<=>()}.
87 To use the new operator, the user project must be compiled in C++20 mode.
88
89 However, starting from Qt 6.7, C++17 users can use
90 \l {Comparison types overview}{our own equivalents} of the std ordering
91 types.
92
93 As a C++20 language feature, backporting \c {operator<=>()} to C++17 is not
94 possible, but you can use the \l Qt::compareThreeWay() function, which acts
95 like a C++17 version of \c {operator<=>()} for built-in C++ types (such as
96 integers, floating-point, and enumeration types).
97
98 Qt also defines helper functions \c {compareThreeWay()} for various classes
99 in QtCore. These are all implemented as \l {https://wg21.link/p1601}
100 {hidden friends}. Users can implement their own \c {compareThreeWay()}
101 functions for custom types.
102
103 Finally, Qt provides a \l qCompareThreeWay() function template, which serves
104 as a generic three-way comparison implementation. It relies on
105 \l {Qt::compareThreeWay()} and the above-mentioned free
106 \c {compareThreeWay()} functions in its implementation.
107
108 \section1 Additional important features
109
110 Qt has also adopted a few more features from C++20, such as:
111
112 \list
113 \li \l Q_CONSTINIT macro that expands to the C++20 \c {constinit}
114 keyword, or to compiler-specific attributes if they are available
115 \li \l Q_NODISCARD_CTOR macro that expands to the \c {[[nodiscard]]}
116 attribute if the compiler supports \c {[[nodiscard]]} on
117 constructors
118 \li \l Q_NODISCARD_X and \l Q_NODISCARD_CTOR_X macros for
119 \c {[[nodiscard("reason")]]}
120 \endlist
121*/