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
qtypes.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// Copyright (C) 2022 Intel Corporation.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4// Qt-Security score:significant reason:default
5
6#include "qtypes.h"
7
8#include <QtCore/qcompilerdetection.h>
9#include <QtCore/qsystemdetection.h>
10#include <QtCore/qprocessordetection.h>
11
12#include <climits>
13#include <limits>
14#include <type_traits>
15
16QT_BEGIN_NAMESPACE
17
18/*!
19 \headerfile <QtTypes>
20 \inmodule QtCore
21 \title Qt Type Declarations
22
23 \brief The <QtTypes> header file includes Qt fundamental type declarations.
24
25 The header file declares several type definitions that guarantee a
26 specified bit-size on all platforms supported by Qt for various
27 basic types, for example \l qint8 which is a signed char
28 guaranteed to be 8-bit on all platforms supported by Qt. The
29 header file also declares the \l qlonglong type definition for
30 \c {long long int}.
31
32 Several convenience type definitions are declared: \l qreal for \c
33 double or \c float, \l uchar for \c {unsigned char}, \l uint for
34 \c {unsigned int}, \l ulong for \c {unsigned long} and \l ushort
35 for \c {unsigned short}.
36
37 The header also provides series of macros that make it possible to print
38 some Qt type aliases (qsizetype, qintptr, etc.) via a formatted output
39 facility such as printf() or qDebug() without raising formatting warnings
40 and without the need of a type cast.
41*/
42
43/*!
44 \typedef qreal
45 \relates <QtTypes>
46
47 Typedef for \c double unless Qt is configured with the
48 \c{-qreal float} option.
49*/
50
51/*! \typedef uchar
52 \relates <QtTypes>
53
54 Convenience typedef for \c{unsigned char}.
55*/
56
57/*! \typedef ushort
58 \relates <QtTypes>
59
60 Convenience typedef for \c{unsigned short}.
61*/
62
63/*! \typedef uint
64 \relates <QtTypes>
65
66 Convenience typedef for \c{unsigned int}.
67*/
68
69/*! \typedef ulong
70 \relates <QtTypes>
71
72 Convenience typedef for \c{unsigned long}.
73*/
74
75/*! \typedef qint8
76 \relates <QtTypes>
77
78 Typedef for \c{signed char}. This type is guaranteed to be 8-bit
79 on all platforms supported by Qt.
80*/
81
82/*!
83 \typedef quint8
84 \relates <QtTypes>
85
86 Typedef for \c{unsigned char}. This type is guaranteed to
87 be 8-bit on all platforms supported by Qt.
88*/
89
90/*! \typedef qint16
91 \relates <QtTypes>
92
93 Typedef for \c{signed short}. This type is guaranteed to be
94 16-bit on all platforms supported by Qt.
95*/
96
97/*!
98 \typedef quint16
99 \relates <QtTypes>
100
101 Typedef for \c{unsigned short}. This type is guaranteed to
102 be 16-bit on all platforms supported by Qt.
103*/
104
105/*! \typedef qint32
106 \relates <QtTypes>
107
108 Typedef for \c{signed int}. This type is guaranteed to be 32-bit
109 on all platforms supported by Qt.
110*/
111
112/*!
113 \typedef quint32
114 \relates <QtTypes>
115
116 Typedef for \c{unsigned int}. This type is guaranteed to
117 be 32-bit on all platforms supported by Qt.
118*/
119
120/*! \typedef qint64
121 \relates <QtTypes>
122
123 Typedef for \c{long long int}. This type is guaranteed to be 64-bit
124 on all platforms supported by Qt.
125
126 Literals of this type can be created using the Q_INT64_C() macro:
127
128 \snippet code/src_corelib_global_qglobal.cpp 5
129
130 \sa Q_INT64_C(), quint64, qlonglong
131*/
132
133/*!
134 \typedef quint64
135 \relates <QtTypes>
136
137 Typedef for \c{unsigned long long int}. This type is guaranteed to
138 be 64-bit on all platforms supported by Qt.
139
140 Literals of this type can be created using the Q_UINT64_C()
141 macro:
142
143 \snippet code/src_corelib_global_qglobal.cpp 6
144
145 \sa Q_UINT64_C(), qint64, qulonglong
146*/
147
148/*!
149 \typedef qint128
150 \relates <QtTypes>
151 \since 6.6
152
153 Typedef for \c{__int128} on platforms that support it (Qt defines the macro
154 \l QT_SUPPORTS_INT128 if this is the case).
155
156 Literals of this type can be created using the Q_INT128_C() macro.
157
158 \sa Q_INT128_C(), Q_INT128_MIN, Q_INT128_MAX, quint128, QT_SUPPORTS_INT128
159*/
160
161/*!
162 \typedef quint128
163 \relates <QtTypes>
164 \since 6.6
165
166 Typedef for \c{unsigned __int128} on platforms that support it (Qt defines
167 the macro \l QT_SUPPORTS_INT128 if this is the case).
168
169 Literals of this type can be created using the Q_UINT128_C() macro.
170
171 \sa Q_UINT128_C(), Q_UINT128_MAX, qint128, QT_SUPPORTS_INT128
172*/
173
174/*!
175 \macro QT_SUPPORTS_INT128
176 \relates <QtTypes>
177 \since 6.6
178
179 Qt defines this macro as well as the \l qint128 and \l quint128 types if
180 the platform has support for 128-bit integer types.
181
182 \sa qint128, quint128, Q_INT128_C(), Q_UINT128_C(), Q_INT128_MIN, Q_INT128_MAX, Q_UINT128_MAX
183*/
184
185/*!
186 \typedef qintptr
187 \relates <QtTypes>
188
189 Integral type for representing pointers in a signed integer (useful for
190 hashing, etc.).
191
192 Typedef for either qint32 or qint64. This type is guaranteed to
193 be the same size as a pointer on all platforms supported by Qt. On
194 a system with 32-bit pointers, qintptr is a typedef for qint32;
195 on a system with 64-bit pointers, qintptr is a typedef for
196 qint64.
197
198 Note that qintptr is signed. Use quintptr for unsigned values.
199
200 In order to print values of this type by using formatted-output
201 facilities such as \c{printf()}, qDebug(), QString::asprintf() and
202 so on, you can use the \c{PRIdQINTPTR} and \c{PRIiQINTPTR}
203 macros as format specifiers. They will both print the value as a
204 base 10 number.
205
206 \code
207 qintptr p = 123;
208 printf("The pointer is %" PRIdQINTPTR "\n", p);
209 \endcode
210
211 \sa qptrdiff, qint32, qint64
212*/
213
214/*! \typedef qlonglong
215 \relates <QtTypes>
216
217 Typedef for \c{long long int} (\c __int64 on Windows). This is
218 the same as \l qint64.
219
220 \sa qulonglong, qint64
221*/
222
223/*!
224 \typedef qulonglong
225 \relates <QtTypes>
226
227 Typedef for \c{unsigned long long int} (\c{unsigned __int64} on
228 Windows). This is the same as \l quint64.
229
230 \sa quint64, qlonglong
231*/
232
233/*!
234 \macro PRIdQINTPTR
235 \macro PRIiQINTPTR
236 \since 6.2
237 \relates <QtTypes>
238
239 See \l qintptr.
240*/
241
242/*!
243 \typedef quintptr
244 \relates <QtTypes>
245
246 Integral type for representing pointers in an unsigned integer (useful for
247 hashing, etc.).
248
249 Typedef for either quint32 or quint64. This type is guaranteed to
250 be the same size as a pointer on all platforms supported by Qt. On
251 a system with 32-bit pointers, quintptr is a typedef for quint32;
252 on a system with 64-bit pointers, quintptr is a typedef for
253 quint64.
254
255 Note that quintptr is unsigned. Use qptrdiff for signed values.
256
257 In order to print values of this type by using formatted-output
258 facilities such as \c{printf()}, qDebug(), QString::asprintf() and
259 so on, you can use the following macros as format specifiers:
260
261 \list
262 \li \c{PRIuQUINTPTR}: prints the value as a base 10 number.
263 \li \c{PRIoQUINTPTR}: prints the value as a base 8 number.
264 \li \c{PRIxQUINTPTR}: prints the value as a base 16 number, using lowercase \c{a-f} letters.
265 \li \c{PRIXQUINTPTR}: prints the value as a base 16 number, using uppercase \c{A-F} letters.
266 \endlist
267
268 \code
269 quintptr p = 123u;
270 printf("The pointer value is 0x%" PRIXQUINTPTR "\n", p);
271 \endcode
272
273 \sa qptrdiff, quint32, quint64
274*/
275
276/*!
277 \macro PRIoQUINTPTR
278 \macro PRIuQUINTPTR
279 \macro PRIxQUINTPTR
280 \macro PRIXQUINTPTR
281 \since 6.2
282 \relates <QtTypes>
283
284 See quintptr.
285*/
286
287/*!
288 \typedef qptrdiff
289 \relates <QtTypes>
290
291 Integral type for representing pointer differences.
292
293 Typedef for either qint32 or qint64. This type is guaranteed to be
294 the same size as a pointer on all platforms supported by Qt. On a
295 system with 32-bit pointers, quintptr is a typedef for quint32; on
296 a system with 64-bit pointers, quintptr is a typedef for quint64.
297
298 Note that qptrdiff is signed. Use quintptr for unsigned values.
299
300 In order to print values of this type by using formatted-output
301 facilities such as \c{printf()}, qDebug(), QString::asprintf() and
302 so on, you can use the \c{PRIdQPTRDIFF} and \c{PRIiQPTRDIFF}
303 macros as format specifiers. They will both print the value as a
304 base 10 number.
305
306 \code
307 qptrdiff d = 123;
308 printf("The difference is %" PRIdQPTRDIFF "\n", d);
309 \endcode
310
311 \sa quintptr, qint32, qint64
312*/
313
314/*!
315 \macro PRIdQPTRDIFF
316 \macro PRIiQPTRDIFF
317 \since 6.2
318 \relates <QtTypes>
319
320 See qptrdiff.
321*/
322
323/*!
324 \typedef qsizetype
325 \relates <QtTypes>
326 \since 5.10
327
328 Integral type providing Posix' \c ssize_t for all platforms.
329
330 This type is guaranteed to be the same size as a \c size_t on all
331 platforms supported by Qt.
332
333 Note that qsizetype is signed. Use \c size_t for unsigned values.
334
335 In order to print values of this type by using formatted-output
336 facilities such as \c{printf()}, qDebug(), QString::asprintf() and
337 so on, you can use the \c{PRIdQSIZETYPE} and \c{PRIiQSIZETYPE}
338 macros as format specifiers. They will both print the value as a
339 base 10 number.
340
341 \code
342 qsizetype s = 123;
343 printf("The size is %" PRIdQSIZETYPE "\n", s);
344 \endcode
345
346 \sa qptrdiff
347*/
348
349/*!
350 \macro PRIdQSIZETYPE
351 \macro PRIiQSIZETYPE
352 \since 6.2
353 \relates <QtTypes>
354
355 See qsizetype.
356*/
357
358/*! \macro qint64 Q_INT64_C(literal)
359 \relates <QtTypes>
360
361 Wraps the signed 64-bit integer \a literal in a
362 platform-independent way.
363
364 Example:
365
366 \snippet code/src_corelib_global_qglobal.cpp 8
367
368 \sa qint64, Q_UINT64_C(), Q_INT128_C()
369*/
370
371/*! \macro quint64 Q_UINT64_C(literal)
372 \relates <QtTypes>
373
374 Wraps the unsigned 64-bit integer \a literal in a
375 platform-independent way.
376
377 Example:
378
379 \snippet code/src_corelib_global_qglobal.cpp 9
380
381 \sa quint64, Q_INT64_C(), Q_UINT128_C()
382*/
383
384/*!
385 \macro qint128 Q_INT128_C(literal)
386 \relates <QtTypes>
387 \since 6.6
388
389 Wraps the signed 128-bit integer \a literal in a
390 platform-independent way.
391
392 \note Unlike Q_INT64_C(), this macro is only available in C++, not in C.
393 This is because compilers do not provide these literals as built-ins and C
394 does not have support for user-defined literals.
395
396 \sa qint128, Q_UINT128_C(), Q_INT128_MIN, Q_INT128_MAX, Q_INT64_C(), QT_SUPPORTS_INT128
397*/
398
399/*!
400 \macro quint128 Q_UINT128_C(literal)
401 \relates <QtTypes>
402 \since 6.6
403
404 Wraps the unsigned 128-bit integer \a literal in a
405 platform-independent way.
406
407 \note Unlike Q_UINT64_C(), this macro is only available in C++, not in C.
408 This is because compilers do not provide these literals as built-ins and C
409 does not have support for user-defined literals.
410
411 \sa quint128, Q_INT128_C(), Q_UINT128_MAX, Q_UINT64_C(), QT_SUPPORTS_INT128
412*/
413
414/*!
415 \macro Q_UINT128_MAX
416 \relates <QtTypes>
417 \since 6.6
418
419 This macro expands to a compile-time constant representing the
420 maximum value representable in a \l quint128.
421
422 This macro is available in both C++ and C modes.
423
424 The minimum of \l quint128 is 0 (zero), so a \c{Q_UINT128_MIN} is neither
425 needed nor provided.
426
427 \sa Q_INT128_MAX, quint128, Q_UINT128_C, QT_SUPPORTS_INT128
428*/
429
430/*!
431 \macro Q_INT128_MIN
432 \relates <QtTypes>
433 \since 6.6
434
435 This macro expands to a compile-time constant representing the
436 minimum value representable in a \l qint128.
437
438 This macro is available in both C++ and C modes.
439
440 \sa Q_INT128_MAX, qint128, Q_INT128_C, QT_SUPPORTS_INT128
441*/
442
443/*!
444 \macro Q_INT128_MAX
445 \relates <QtTypes>
446 \since 6.6
447
448 This macro expands to a compile-time constant representing the
449 maximum value representable in a \l qint128.
450
451 This macro is available in both C++ and C modes.
452
453 \sa Q_INT128_MIN, Q_UINT128_MAX, qint128, Q_INT128_C, QT_SUPPORTS_INT128
454*/
455
456// Statically check assumptions about the environment we're running
457// in. The idea here is to error or warn if otherwise implicit Qt
458// assumptions are not fulfilled on new hardware or compilers
459// (if this list becomes too long, consider factoring into a separate file)
460static_assert(UCHAR_MAX == 255, "Qt assumes that char is 8 bits");
461static_assert(sizeof(int) == 4, "Qt assumes that int is 32 bits");
462static_assert(QT_POINTER_SIZE == sizeof(void *), "QT_POINTER_SIZE defined incorrectly");
463static_assert(sizeof(float) == 4, "Qt assumes that float is 32 bits");
464static_assert(sizeof(char16_t) == 2, "Qt assumes that char16_t is 16 bits");
465static_assert(sizeof(char32_t) == 4, "Qt assumes that char32_t is 32 bits");
466#if defined(Q_OS_WIN)
467static_assert(sizeof(wchar_t) == sizeof(char16_t));
468#endif
469static_assert(std::numeric_limits<int>::radix == 2,
470 "Qt assumes binary integers");
471static_assert((std::numeric_limits<int>::max() + std::numeric_limits<int>::lowest()) == -1,
472 "Qt assumes two's complement integers");
473static_assert(sizeof(wchar_t) == sizeof(char32_t) || sizeof(wchar_t) == sizeof(char16_t),
474 "Qt assumes wchar_t is compatible with either char32_t or char16_t");
475
476// While we'd like to check for __STDC_IEC_559__, as per ISO/IEC 9899:2011
477// Annex F (C11, normative for C++11), there are a few corner cases regarding
478// denormals where GHS compiler is relying hardware behavior that is not IEC
479// 559 compliant. So split the check in several subchecks.
480
481// On GHS the compiler reports std::numeric_limits<float>::is_iec559 as false.
482// This is all right according to our needs.
483#if !defined(Q_CC_GHS)
484static_assert(std::numeric_limits<float>::is_iec559,
485 "Qt assumes IEEE 754 floating point");
486#endif
487
488// Technically, presence of NaN and infinities are implied from the above check,
489// but double checking our environment doesn't hurt...
490static_assert(std::numeric_limits<float>::has_infinity &&
491 std::numeric_limits<float>::has_quiet_NaN,
492 "Qt assumes IEEE 754 floating point");
493
494// is_iec559 checks for ISO/IEC/IEEE 60559:2011 (aka IEEE 754-2008) compliance,
495// but that allows for a non-binary radix. We need to recheck that.
496// Note how __STDC_IEC_559__ would instead check for IEC 60559:1989, aka
497// ANSI/IEEE 754−1985, which specifically implies binary floating point numbers.
498static_assert(std::numeric_limits<float>::radix == 2,
499 "Qt assumes binary IEEE 754 floating point");
500
501// not required by the definition of size_t, but we depend on this
502static_assert(sizeof(size_t) == sizeof(void *), "size_t and a pointer don't have the same size");
503static_assert(sizeof(size_t) == sizeof(qsizetype)); // implied by the definition
504static_assert((std::is_same<qsizetype, qptrdiff>::value));
505static_assert(std::is_same_v<std::size_t, size_t>);
506
507#if defined(QT_COMPILER_SUPPORTS_INT128) && !defined(QT_NO_INT128)
508# ifndef QT_SUPPORTS_INT128
509# error Qt needs to be compiled in a mode that enables INT128
510 if the compiler supports it in principle and QT_NO_INT128 is not defined.
511# endif
512#endif
513
514// Check that our own typedefs are not broken.
515static_assert(sizeof(qint8) == 1, "Internal error, qint8 is misdefined");
516static_assert(sizeof(qint16)== 2, "Internal error, qint16 is misdefined");
517static_assert(sizeof(qint32) == 4, "Internal error, qint32 is misdefined");
518static_assert(sizeof(qint64) == 8, "Internal error, qint64 is misdefined");
519#ifdef QT_SUPPORTS_INT128
520static_assert(sizeof(qint128) == 16, "Internal error, qint128 is misdefined");
521#endif
522
523#ifdef QT_SUPPORTS_INT128
524// Standard Library supports for 128-bit integers:
525// Implementation | Version | Note
526// ---------------------|---------|------
527// GNU libstdc++ | 11.1.0 |
528// LLVM libc++ | 3.5 | May change if compiler has __is_integral()
529// MS STL | none |
530
531# if defined(_LIBCPP_VERSION) || (defined(_GLIBCXX_RELEASE) && _GLIBCXX_RELEASE >= 11)
532static_assert(std::numeric_limits<quint128>::max() == Q_UINT128_MAX);
533# endif
534#endif
535
536QT_END_NAMESPACE
short qint16
Definition qtypes.h:53
int qint32
Definition qtypes.h:55
long long qint64
Definition qtypes.h:66
QT_BEGIN_NAMESPACE typedef signed char qint8
Definition qtypes.h:51