1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
5 \headerfile <QtCompilerDetection>
7 \title Compiler-specific Macro Definitions
10 \brief The <QtCompilerDetection> header file includes various
11 compiler-specific macros.
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++.
18 The purpose of these macros is to enable programmers to add
19 compiler-specific code to their application.
24 \relates <QtCompilerDetection>
26 Defined if the application is compiled using Digital Mars C/C++
27 (used to be Symantec C++).
32 \relates <QtCompilerDetection>
34 Defined if the application is compiled using Microsoft Visual
35 C/C++, Intel C++ for Windows.
40 \relates <QtCompilerDetection>
42 Defined if the application is compiled using Clang.
47 \relates <QtCompilerDetection>
49 Defined if the application is compiled using Borland/Turbo C++.
54 \relates <QtCompilerDetection>
56 Defined if the application is compiled using Watcom C++.
61 \relates <QtCompilerDetection>
63 Defined if the application is compiled using GNU Compiler Collection (GCC).
68 \relates <QtCompilerDetection>
70 Defined if the application is compiled using Comeau C++.
75 \relates <QtCompilerDetection>
77 Defined if the application is compiled using Edison Design Group
83 \relates <QtCompilerDetection>
85 Defined if the application is compiled using CenterLine C++.
90 \relates <QtCompilerDetection>
92 Defined if the application is compiled using Forte Developer, or
98 \relates <QtCompilerDetection>
100 Defined if the application is compiled using MIPSpro C++.
105 \relates <QtCompilerDetection>
107 Defined if the application is compiled using DEC C++.
112 \relates <QtCompilerDetection>
114 Defined if the application is compiled using HP aC++.
119 \relates <QtCompilerDetection>
121 Defined if the application is compiled using SCO OUDK and UDK.
126 \relates <QtCompilerDetection>
128 Defined if the application is compiled using Reliant C++.
133 \relates <QtCompilerDetection>
135 Defined if the application is compiled using KAI C++.
140 \relates <QtCompilerDetection>
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.
152 \relates <QtCompilerDetection>
154 Defined if the application is compiled using MetaWare High C/C++.
159 \relates <QtCompilerDetection>
161 Defined if the application is compiled using Portland Group C++.
166 \relates <QtCompilerDetection>
168 Defined if the application is compiled using Green Hills
169 Optimizing C++ Compilers.
173 \macro void Q_FALLTHROUGH()
174 \relates <QtCompilerDetection>
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.
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.
183 \sa Q_UNREACHABLE(), Q_UNREACHABLE_RETURN()
187 \macro Q_LIKELY(expr)
188 \relates <QtCompilerDetection>
191 \brief Hints to the compiler that the enclosed condition, \a expr, is
192 likely to evaluate to \c true.
194 Use of this macro can help the compiler to optimize the code.
198 \snippet code/src_corelib_global_qglobal.cpp qlikely
204 \macro Q_UNLIKELY(expr)
205 \relates <QtCompilerDetection>
208 \brief Hints to the compiler that the enclosed condition, \a expr, is
209 likely to evaluate to \c false.
211 Use of this macro can help the compiler to optimize the code.
215 \snippet code/src_corelib_global_qglobal.cpp qunlikely
222 \relates <QtCompilerDetection>
225 \brief Enforces constant initialization when supported by the compiler.
227 If the compiler supports the C++20 \c{constinit} keyword, Clang's
228 \c{[[clang::require_constant_initialization]]} or GCC's \c{__constinit},
229 then this macro expands to the first one of these that is available,
230 otherwise it expands to nothing.
232 Variables marked as \c{constinit} cause a compile-error if their
233 initialization would have to be performed at runtime.
235 \note Constant-initialized variables may still have load-time impact if
236 they have non-trivial destruction.
238 For constants, you can use \c{constexpr} since C++11, but \c{constexpr}
239 makes variables \c{const}, too, whereas \c{constinit} ensures constant
240 initialization, but doesn't make the variable \c{const}:
243 \header \li Keyword \li Added \li immutable \li constant-initialized
244 \row \li \c{const} \li C++98 \li yes \li not required
245 \row \li \c{constexpr} \li C++11 \li yes \li required
246 \row \li \c{constinit} \li C++20 \li no \li required
252 \relates <QtCompilerDetection>
254 This macro marks a symbol for shared library export (see
255 \l{sharedlibrary.html}{Creating Shared Libraries}).
262 \relates <QtCompilerDetection>
264 This macro declares a symbol to be an import from a shared library (see
265 \l{sharedlibrary.html}{Creating Shared Libraries}).
271 \macro Q_DECL_CONSTEXPR
272 \relates <QtCompilerDetection>
273 \deprecated [6.4] Use the \c constexpr keyword instead.
275 This macro can be used to declare variable that should be constructed at compile-time,
276 or an inline function that can be computed at compile-time.
278 \sa Q_DECL_RELAXED_CONSTEXPR
282 \macro Q_DECL_RELAXED_CONSTEXPR
283 \relates <QtCompilerDetection>
284 \deprecated [6.4] Use the \c constexpr keyword instead.
286 This macro can be used to declare an inline function that can be computed
287 at compile-time according to the relaxed rules from C++14.
293 \macro Q_DECL_NOTHROW
294 \relates <QtCompilerDetection>
296 \deprecated [6.4] Use the \c noexcept keyword instead.
298 This macro marks a function as never throwing, under no
299 circumstances. If the function does nevertheless throw, the
300 behavior is undefined.
302 \sa Q_DECL_NOEXCEPT, Q_DECL_NOEXCEPT_EXPR()
306 \macro Q_DECL_NOEXCEPT
307 \relates <QtCompilerDetection>
309 \deprecated [6.4] Use the \c noexcept keyword instead.
311 This macro marks a function as never throwing. If the function
312 does nevertheless throw, the behavior is defined:
313 std::terminate() is called.
316 \sa Q_DECL_NOTHROW, Q_DECL_NOEXCEPT_EXPR()
320 \macro Q_DECL_NOEXCEPT_EXPR(x)
321 \relates <QtCompilerDetection>
323 \deprecated [6.4] Use the \c noexcept keyword instead.
325 This macro marks a function as non-throwing if \a x is \c true. If
326 the function does nevertheless throw, the behavior is defined:
327 std::terminate() is called.
330 \sa Q_DECL_NOTHROW, Q_DECL_NOEXCEPT
334 \macro Q_DECL_OVERRIDE
337 \relates <QtCompilerDetection>
339 This macro can be used to declare an overriding virtual
340 function. Use of this markup will allow the compiler to generate
341 an error if the overriding virtual function does not in fact
344 It expands to "override".
346 The macro goes at the end of the function, usually after the
348 \snippet code/src_corelib_global_qglobal.cpp qdecloverride
357 \relates <QtCompilerDetection>
359 This macro can be used to declare an overriding virtual or a class
360 as "final", with Java semantics. Further-derived classes can then
361 no longer override this virtual function, or inherit from this
364 It expands to "final".
366 The macro goes at the end of the function, usually after the
368 \snippet code/src_corelib_global_qglobal.cpp qdeclfinal-1
370 For classes, it goes in front of the \c{:} in the class
372 \snippet code/src_corelib_global_qglobal.cpp qdeclfinal-2
378 \macro const char* Q_FUNC_INFO()
379 \relates <QtCompilerDetection>
381 Expands to a string that describe the function the macro resides in. How this string looks
382 more specifically is compiler dependent. With GNU GCC it is typically the function signature,
383 while with other compilers it might be the line and column number.
385 Q_FUNC_INFO can be conveniently used with qDebug(). For example, this function:
387 \snippet code/src_corelib_global_qglobal.cpp 22
389 when instantiated with the integer type, will with the GCC compiler produce:
391 \tt{const TInputType& myMin(const TInputType&, const TInputType&) [with TInputType = int] was called with value1: 3 value2: 4}
393 If this macro is used outside a function, the behavior is undefined.
397 \macro Q_NODISCARD_CTOR
398 \relates <QtCompilerDetection>
401 \brief Expands to \c{[[nodiscard]]} on compilers that accept it on constructors.
403 Otherwise it expands to nothing.
405 Constructors marked as Q_NODISCARD_CTOR cause a compiler warning if a call
406 site doesn't use the resulting object.
408 This macro is exists solely to prevent warnings on compilers that don't
409 implement the feature. If your supported platforms all allow \c{[[nodiscard]]}
410 on constructors, we strongly recommend you use the C++ attribute directly instead
413 \sa Q_NODISCARD_CTOR_X
417 \macro Q_NODISCARD_X(message)
418 \macro Q_NODISCARD_CTOR_X(message)
419 \relates <QtCompilerDetection>
422 \brief Expand to \c{[[nodiscard(message)]]} on compilers that support the feature.
424 Otherwise they expand to [[nodiscard]] and Q_NODISCARD_CTOR, respectively.