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
282/*!
283 \macro Q_DECL_EXPORT
284 \relates <QtCompilerDetection>
285
286 This macro marks a symbol for shared library export (see
287 \l{sharedlibrary.html}{Creating Shared Libraries}).
288
289 \sa Q_DECL_IMPORT
290*/
291
292/*!
293 \macro Q_DECL_IMPORT
294 \relates <QtCompilerDetection>
295
296 This macro declares a symbol to be an import from a shared library (see
297 \l{sharedlibrary.html}{Creating Shared Libraries}).
298
299 \sa Q_DECL_EXPORT
300*/
301
302/*!
303 \macro Q_DECL_CONSTEXPR
304 \relates <QtCompilerDetection>
305 \deprecated [6.4] Use the \c constexpr keyword instead.
306
307 This macro can be used to declare variable that should be constructed at compile-time,
308 or an inline function that can be computed at compile-time.
309
310 \sa Q_DECL_RELAXED_CONSTEXPR, Q_DECL_CONSTEXPR_DTOR
311*/
312
313/*!
314 \macro Q_DECL_RELAXED_CONSTEXPR
315 \relates <QtCompilerDetection>
316 \deprecated [6.4] Use the \c constexpr keyword instead.
317
318 This macro can be used to declare an inline function that can be computed
319 at compile-time according to the relaxed rules from C++14.
320
321 \sa Q_DECL_CONSTEXPR, Q_DECL_CONSTEXPR_DTOR
322*/
323
324/*!
325 \macro Q_DECL_CONSTEXPR_DTOR
326 \relates <QtCompilerDetection>
327 \since 6.9
328
329 Expands to \c{constexpr} on compilers that support C++20, where
330 \c{__cpp_constexpr} has a value greater than or equal to \c{201907L}.
331 Otherwise, it expands to \c{inline}.
332
333 Use this macro to declare a destructor that can be computed at
334 compile-time in C++20 or later.
335
336 \sa Q_CONSTEXPR_DTOR
337*/
338
339/*!
340 \macro Q_CONSTEXPR_DTOR
341 \relates <QtCompilerDetection>
342 \since 6.9
343
344 Expands to \c{constexpr} on compilers that support C++20, where
345 \c{__cpp_constexpr} has a value greater than or equal to \c{201907L}.
346 Otherwise, it expands to \c{const}.
347
348 Use this macro to declare a variable that can be constructed at compile-time
349 in C++20 or later.
350
351 \sa Q_DECL_CONSTEXPR_DTOR
352*/
353
354/*!
355 \macro Q_DECL_EQ_DELETE_X(reason)
356 \relates <QtCompilerDetection>
357 \since 6.9
358
359 Expands to \c{= delete(reason)} on compilers that support C++26, where
360 \c{__cpp_deleted_function} has a value greater than or equal to \c{202403L}.
361 Otherwise, it expands to \c{= delete}.
362
363 Use this macro to declare a deleted function and also supply an error
364 message (the \a reason) to the user in case the function gets used
365 (explaining why the function was deleted).
366*/
367
368/*!
369 \macro Q_DECL_NOTHROW
370 \relates <QtCompilerDetection>
371 \since 5.0
372 \deprecated [6.4] Use the \c noexcept keyword instead.
373
374 This macro marks a function as never throwing, under no
375 circumstances. If the function does nevertheless throw, the
376 behavior is undefined.
377
378 \sa Q_DECL_NOEXCEPT, Q_DECL_NOEXCEPT_EXPR()
379*/
380
381/*!
382 \macro Q_DECL_NOEXCEPT
383 \relates <QtCompilerDetection>
384 \since 5.0
385 \deprecated [6.4] Use the \c noexcept keyword instead.
386
387 This macro marks a function as never throwing. If the function
388 does nevertheless throw, the behavior is defined:
389 std::terminate() is called.
390
391
392 \sa Q_DECL_NOTHROW, Q_DECL_NOEXCEPT_EXPR()
393*/
394
395/*!
396 \macro Q_DECL_NOEXCEPT_EXPR(x)
397 \relates <QtCompilerDetection>
398 \since 5.0
399 \deprecated [6.4] Use the \c noexcept keyword instead.
400
401 This macro marks a function as non-throwing if \a x is \c true. If
402 the function does nevertheless throw, the behavior is defined:
403 std::terminate() is called.
404
405
406 \sa Q_DECL_NOTHROW, Q_DECL_NOEXCEPT
407*/
408
409/*!
410 \macro Q_DECL_OVERRIDE
411 \since 5.0
412 \deprecated
413 \relates <QtCompilerDetection>
414
415 This macro can be used to declare an overriding virtual
416 function. Use of this markup will allow the compiler to generate
417 an error if the overriding virtual function does not in fact
418 override anything.
419
420 It expands to "override".
421
422 The macro goes at the end of the function, usually after the
423 \c{const}, if any:
424 \snippet code/src_corelib_global_qglobal.cpp qdecloverride
425
426 \sa Q_DECL_FINAL
427*/
428
429/*!
430 \macro Q_DECL_FINAL
431 \since 5.0
432 \deprecated
433 \relates <QtCompilerDetection>
434
435 This macro can be used to declare an overriding virtual or a class
436 as "final", with Java semantics. Further-derived classes can then
437 no longer override this virtual function, or inherit from this
438 class, respectively.
439
440 It expands to "final".
441
442 The macro goes at the end of the function, usually after the
443 \c{const}, if any:
444 \snippet code/src_corelib_global_qglobal.cpp qdeclfinal-1
445
446 For classes, it goes in front of the \c{:} in the class
447 definition, if any:
448 \snippet code/src_corelib_global_qglobal.cpp qdeclfinal-2
449
450 \sa Q_DECL_OVERRIDE
451*/
452
453/*!
454 \macro const char* Q_FUNC_INFO()
455 \relates <QtCompilerDetection>
456
457 Expands to a string that describe the function the macro resides in. How this string looks
458 more specifically is compiler dependent. With GNU GCC it is typically the function signature,
459 while with other compilers it might be the line and column number.
460
461 Q_FUNC_INFO can be conveniently used with qDebug(). For example, this function:
462
463 \snippet code/src_corelib_global_qglobal.cpp 22
464
465 when instantiated with the integer type, will with the GCC compiler produce:
466
467 \tt{const TInputType& myMin(const TInputType&, const TInputType&) [with TInputType = int] was called with value1: 3 value2: 4}
468
469 If this macro is used outside a function, the behavior is undefined.
470*/
471
472/*!
473 \macro Q_NODISCARD_CTOR
474 \relates <QtCompilerDetection>
475 \since 6.6
476
477 \brief Expands to \c{[[nodiscard]]} on compilers that accept it on constructors.
478
479 Otherwise it expands to nothing.
480
481 Constructors marked as Q_NODISCARD_CTOR cause a compiler warning if a call
482 site doesn't use the resulting object.
483
484 This macro is exists solely to prevent warnings on compilers that don't
485 implement the feature. If your supported platforms all allow \c{[[nodiscard]]}
486 on constructors, we strongly recommend you use the C++ attribute directly instead
487 of this macro.
488
489 \sa Q_NODISCARD_CTOR_X
490*/
491
492/*!
493 \macro Q_NODISCARD_X(message)
494 \macro Q_NODISCARD_CTOR_X(message)
495 \relates <QtCompilerDetection>
496 \since 6.7
497
498 \brief Expand to \c{[[nodiscard(message)]]} on compilers that support the feature.
499
500 Otherwise they expand to \c {[[nodiscard]]} and Q_NODISCARD_CTOR, respectively.
501
502 \sa Q_NODISCARD_CTOR
503*/