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
qcompilerdetection.qdoc
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5 \headerfile <QtCompilerDetection>
6 \inmodule QtCore
7 \title Compiler-specific Macro Definitions
8 \ingroup funclists
9
10 \brief The <QtCompilerDetection> header file includes various
11 compiler-specific macros.
12
13 The <QtCompilerDetection> header file provides a range of macros (Q_CC_*)
14 that are defined if the application is compiled using the specified
15 compiler. For example, the Q_CC_SUN macro is defined if the application is
16 compiled using Forte Developer, or Sun Studio C++.
17
18 The purpose of these macros is to enable programmers to add
19 compiler-specific code to their application.
20*/
21
22/*!
23 \macro Q_CC_SYM
24 \relates <QtCompilerDetection>
25
26 Defined if the application is compiled using Digital Mars C/C++
27 (used to be Symantec C++).
28*/
29
30/*!
31 \macro Q_CC_MSVC
32 \relates <QtCompilerDetection>
33
34 Defined if the application is compiled using Microsoft Visual
35 C/C++, Intel C++ for Windows.
36*/
37
38/*!
39 \macro Q_CC_CLANG
40 \relates <QtCompilerDetection>
41
42 Defined if the application is compiled using Clang.
43*/
44
45/*!
46 \macro Q_CC_BOR
47 \relates <QtCompilerDetection>
48
49 Defined if the application is compiled using Borland/Turbo C++.
50*/
51
52/*!
53 \macro Q_CC_WAT
54 \relates <QtCompilerDetection>
55
56 Defined if the application is compiled using Watcom C++.
57*/
58
59/*!
60 \macro Q_CC_GNU
61 \relates <QtCompilerDetection>
62
63 Defined if the application is compiled using GNU Compiler Collection (GCC).
64*/
65
66/*!
67 \macro Q_CC_COMEAU
68 \relates <QtCompilerDetection>
69
70 Defined if the application is compiled using Comeau C++.
71*/
72
73/*!
74 \macro Q_CC_EDG
75 \relates <QtCompilerDetection>
76
77 Defined if the application is compiled using Edison Design Group
78 C++.
79*/
80
81/*!
82 \macro Q_CC_OC
83 \relates <QtCompilerDetection>
84
85 Defined if the application is compiled using CenterLine C++.
86*/
87
88/*!
89 \macro Q_CC_SUN
90 \relates <QtCompilerDetection>
91
92 Defined if the application is compiled using Forte Developer, or
93 Sun Studio C++.
94*/
95
96/*!
97 \macro Q_CC_MIPS
98 \relates <QtCompilerDetection>
99
100 Defined if the application is compiled using MIPSpro C++.
101*/
102
103/*!
104 \macro Q_CC_DEC
105 \relates <QtCompilerDetection>
106
107 Defined if the application is compiled using DEC C++.
108*/
109
110/*!
111 \macro Q_CC_HPACC
112 \relates <QtCompilerDetection>
113
114 Defined if the application is compiled using HP aC++.
115*/
116
117/*!
118 \macro Q_CC_USLC
119 \relates <QtCompilerDetection>
120
121 Defined if the application is compiled using SCO OUDK and UDK.
122*/
123
124/*!
125 \macro Q_CC_CDS
126 \relates <QtCompilerDetection>
127
128 Defined if the application is compiled using Reliant C++.
129*/
130
131/*!
132 \macro Q_CC_KAI
133 \relates <QtCompilerDetection>
134
135 Defined if the application is compiled using KAI C++.
136*/
137
138/*!
139 \macro Q_CC_INTEL
140 \relates <QtCompilerDetection>
141 \obsolete
142
143 This macro used to be defined if the application was compiled with the old
144 Intel C++ compiler for Linux, macOS or Windows. The new oneAPI C++ compiler
145 is just a build of Clang and therefore does not define this macro.
146
147 \sa Q_CC_CLANG
148*/
149
150/*!
151 \macro Q_CC_HIGHC
152 \relates <QtCompilerDetection>
153
154 Defined if the application is compiled using MetaWare High C/C++.
155*/
156
157/*!
158 \macro Q_CC_PGI
159 \relates <QtCompilerDetection>
160
161 Defined if the application is compiled using Portland Group C++.
162*/
163
164/*!
165 \macro Q_CC_GHS
166 \relates <QtCompilerDetection>
167
168 Defined if the application is compiled using Green Hills
169 Optimizing C++ Compilers.
170*/
171
172/*!
173 \macro void Q_FALLTHROUGH()
174 \relates <QtCompilerDetection>
175 \since 5.8
176
177 Can be used in switch statements at the end of case block to tell the compiler
178 and other developers that the lack of a break statement is intentional.
179
180 This is useful since a missing break statement is often a bug, and some
181 compilers can be configured to emit warnings when one is not found.
182
183 \sa Q_UNREACHABLE(), Q_UNREACHABLE_RETURN()
184*/
185
186/*!
187 \macro Q_LIKELY(expr)
188 \relates <QtCompilerDetection>
189 \since 4.8
190
191 \brief Hints to the compiler that the enclosed condition, \a expr, is
192 likely to evaluate to \c true.
193
194 Use of this macro can help the compiler to optimize the code.
195
196 Example:
197
198 \snippet code/src_corelib_global_qglobal.cpp qlikely
199
200 \sa Q_UNLIKELY(), Q_LIKELY_BRANCH, Q_UNLIKELY_BRANCH
201*/
202
203/*!
204 \macro Q_UNLIKELY(expr)
205 \relates <QtCompilerDetection>
206 \since 4.8
207
208 \brief Hints to the compiler that the enclosed condition, \a expr, is
209 likely to evaluate to \c false.
210
211 Use of this macro can help the compiler to optimize the code.
212
213 Example:
214
215 \snippet code/src_corelib_global_qglobal.cpp qunlikely
216
217 \sa Q_LIKELY(), Q_LIKELY_BRANCH, Q_UNLIKELY_BRANCH
218*/
219
220/*!
221 \macro Q_LIKELY_BRANCH
222 \relates <QtCompilerDetection>
223 \since 6.9
224
225 \brief Hints to the compiler that the following block is likely to be
226 executed.
227
228 Use of this macro can help the compiler to optimize the code.
229
230 It is defined as the C++20 attribute \c{[[likely]]}, and is a no-op on
231 compilers that don't support it.
232
233 \sa Q_LIKELY(), Q_UNLIKELY(), Q_UNLIKELY_BRANCH
234*/
235
236/*!
237 \macro Q_UNLIKELY_BRANCH
238 \relates <QtCompilerDetection>
239 \since 6.9
240
241 \brief Hints to the compiler that the following block is unlikely to be
242 executed.
243
244 Use of this macro can help the compiler to optimize the code.
245
246 It is defined as the C++20 attribute \c{[[unlikely]]}, and is a no-op on
247 compilers that don't support it.
248
249 \sa Q_LIKELY(), Q_UNLIKELY(), Q_LIKELY_BRANCH
250*/
251
252/*!
253 \macro Q_CONSTINIT
254 \relates <QtCompilerDetection>
255 \since 6.4
256
257 \brief Enforces constant initialization when supported by the compiler.
258
259 If the compiler supports the C++20 \c{constinit} keyword, Clang's
260 \c{[[clang::require_constant_initialization]]} or GCC's \c{__constinit},
261 then this macro expands to the first one of these that is available,
262 otherwise it expands to nothing.
263
264 Variables marked as \c{constinit} cause a compile-error if their
265 initialization would have to be performed at runtime.
266
267 \note Constant-initialized variables may still have load-time impact if
268 they have non-trivial destruction.
269
270 For constants, you can use \c{constexpr} since C++11, but \c{constexpr}
271 makes variables \c{const}, too, whereas \c{constinit} ensures constant
272 initialization, but doesn't make the variable \c{const}:
273
274 \table
275 \header \li Keyword \li Added \li immutable \li constant-initialized
276 \row \li \c{const} \li C++98 \li yes \li not required
277 \row \li \c{constexpr} \li C++11 \li yes \li required
278 \row \li \c{constinit} \li C++20 \li no \li required
279 \endtable
280
281 If declaration and definition of a Q_CONSTINIT variable are separate, this
282 macro goes only on the definition, and not on the declaration:
283
284 \code
285 class C {
286 ~~~~
287 static int count; // declaration, no Q_CONSTINIT here
288 };
289 ~~~
290 Q_CONSTINIT int C::count = 0; // definition
291 \endcode
292*/
293
294/*!
295 \macro Q_DECL_EXPORT
296 \relates <QtCompilerDetection>
297
298 This macro marks a symbol for shared library export (see
299 \l{sharedlibrary.html}{Creating Shared Libraries}).
300
301 \sa Q_DECL_IMPORT
302*/
303
304/*!
305 \macro Q_DECL_IMPORT
306 \relates <QtCompilerDetection>
307
308 This macro declares a symbol to be an import from a shared library (see
309 \l{sharedlibrary.html}{Creating Shared Libraries}).
310
311 \sa Q_DECL_EXPORT
312*/
313
314/*!
315 \macro Q_DECL_CONSTEXPR
316 \relates <QtCompilerDetection>
317 \deprecated [6.4] Use the \c constexpr keyword instead.
318
319 This macro can be used to declare variable that should be constructed at compile-time,
320 or an inline function that can be computed at compile-time.
321
322 \sa Q_DECL_RELAXED_CONSTEXPR, Q_DECL_CONSTEXPR_DTOR
323*/
324
325/*!
326 \macro Q_DECL_RELAXED_CONSTEXPR
327 \relates <QtCompilerDetection>
328 \deprecated [6.4] Use the \c constexpr keyword instead.
329
330 This macro can be used to declare an inline function that can be computed
331 at compile-time according to the relaxed rules from C++14.
332
333 \sa Q_DECL_CONSTEXPR, Q_DECL_CONSTEXPR_DTOR
334*/
335
336/*!
337 \macro Q_DECL_CONSTEXPR_DTOR
338 \relates <QtCompilerDetection>
339 \since 6.9
340
341 Expands to \c{constexpr} on compilers that support C++20, where
342 \c{__cpp_constexpr} has a value greater than or equal to \c{201907L}.
343 Otherwise, it expands to \c{inline}.
344
345 Use this macro to declare a destructor that can be computed at
346 compile-time in C++20 or later.
347
348 \sa Q_CONSTEXPR_DTOR
349*/
350
351/*!
352 \macro Q_CONSTEXPR_DTOR
353 \relates <QtCompilerDetection>
354 \since 6.9
355
356 Expands to \c{constexpr} on compilers that support C++20, where
357 \c{__cpp_constexpr} has a value greater than or equal to \c{201907L}.
358 Otherwise, it expands to \c{const}.
359
360 Use this macro to declare a variable that can be constructed at compile-time
361 in C++20 or later.
362
363 \sa Q_DECL_CONSTEXPR_DTOR
364*/
365
366/*!
367 \macro Q_DECL_EQ_DELETE_X(reason)
368 \relates <QtCompilerDetection>
369 \since 6.9
370
371 Expands to \c{= delete(reason)} on compilers that support C++26, where
372 \c{__cpp_deleted_function} has a value greater than or equal to \c{202403L}.
373 Otherwise, it expands to \c{= delete}.
374
375 Use this macro to declare a deleted function and also supply an error
376 message (the \a reason) to the user in case the function gets used
377 (explaining why the function was deleted).
378*/
379
380/*!
381 \macro Q_DECL_NOTHROW
382 \relates <QtCompilerDetection>
383 \since 5.0
384 \deprecated [6.4] Use the \c noexcept keyword instead.
385
386 This macro marks a function as never throwing, under no
387 circumstances. If the function does nevertheless throw, the
388 behavior is undefined.
389
390 \sa Q_DECL_NOEXCEPT, Q_DECL_NOEXCEPT_EXPR()
391*/
392
393/*!
394 \macro Q_DECL_NOEXCEPT
395 \relates <QtCompilerDetection>
396 \since 5.0
397 \deprecated [6.4] Use the \c noexcept keyword instead.
398
399 This macro marks a function as never throwing. If the function
400 does nevertheless throw, the behavior is defined:
401 std::terminate() is called.
402
403
404 \sa Q_DECL_NOTHROW, Q_DECL_NOEXCEPT_EXPR()
405*/
406
407/*!
408 \macro Q_DECL_NOEXCEPT_EXPR(x)
409 \relates <QtCompilerDetection>
410 \since 5.0
411 \deprecated [6.4] Use the \c noexcept keyword instead.
412
413 This macro marks a function as non-throwing if \a x is \c true. If
414 the function does nevertheless throw, the behavior is defined:
415 std::terminate() is called.
416
417
418 \sa Q_DECL_NOTHROW, Q_DECL_NOEXCEPT
419*/
420
421/*!
422 \macro Q_DECL_OVERRIDE
423 \since 5.0
424 \deprecated
425 \relates <QtCompilerDetection>
426
427 This macro can be used to declare an overriding virtual
428 function. Use of this markup will allow the compiler to generate
429 an error if the overriding virtual function does not in fact
430 override anything.
431
432 It expands to "override".
433
434 The macro goes at the end of the function, usually after the
435 \c{const}, if any:
436 \snippet code/src_corelib_global_qglobal.cpp qdecloverride
437
438 \sa Q_DECL_FINAL
439*/
440
441/*!
442 \macro Q_DECL_FINAL
443 \since 5.0
444 \deprecated
445 \relates <QtCompilerDetection>
446
447 This macro can be used to declare an overriding virtual or a class
448 as "final", with Java semantics. Further-derived classes can then
449 no longer override this virtual function, or inherit from this
450 class, respectively.
451
452 It expands to "final".
453
454 The macro goes at the end of the function, usually after the
455 \c{const}, if any:
456 \snippet code/src_corelib_global_qglobal.cpp qdeclfinal-1
457
458 For classes, it goes in front of the \c{:} in the class
459 definition, if any:
460 \snippet code/src_corelib_global_qglobal.cpp qdeclfinal-2
461
462 \sa Q_DECL_OVERRIDE
463*/
464
465/*!
466 \macro const char* Q_FUNC_INFO()
467 \relates <QtCompilerDetection>
468
469 Expands to a string that describe the function the macro resides in. How this string looks
470 more specifically is compiler dependent. With GNU GCC it is typically the function signature,
471 while with other compilers it might be the line and column number.
472
473 Q_FUNC_INFO can be conveniently used with qDebug(). For example, this function:
474
475 \snippet code/src_corelib_global_qglobal.cpp 22
476
477 when instantiated with the integer type, will with the GCC compiler produce:
478
479 \tt{const TInputType& myMin(const TInputType&, const TInputType&) [with TInputType = int] was called with value1: 3 value2: 4}
480
481 If this macro is used outside a function, the behavior is undefined.
482*/
483
484/*!
485 \macro Q_NODISCARD_CTOR
486 \relates <QtCompilerDetection>
487 \since 6.6
488
489 \brief Expands to \c{[[nodiscard]]} on compilers that accept it on constructors.
490
491 Otherwise it expands to nothing.
492
493 Constructors marked as Q_NODISCARD_CTOR cause a compiler warning if a call
494 site doesn't use the resulting object.
495
496 This macro is exists solely to prevent warnings on compilers that don't
497 implement the feature. If your supported platforms all allow \c{[[nodiscard]]}
498 on constructors, we strongly recommend you use the C++ attribute directly instead
499 of this macro.
500
501 \sa Q_NODISCARD_CTOR_X
502*/
503
504/*!
505 \macro Q_NODISCARD_X(message)
506 \macro Q_NODISCARD_CTOR_X(message)
507 \relates <QtCompilerDetection>
508 \since 6.7
509
510 \brief Expand to \c{[[nodiscard(message)]]} on compilers that support the feature.
511
512 Otherwise they expand to \c {[[nodiscard]]} and Q_NODISCARD_CTOR, respectively.
513
514 \sa Q_NODISCARD_CTOR
515*/