<QtCompilerDetection>
The <QtCompilerDetection> header file includes various compiler-specific macros. More...
Header: | #include <QtCompilerDetection> |
Macros
Detailed Description
\inmodule
QtCore \title
Compiler-specific Macro Definitions \ingroup
funclists
The <QtCompilerDetection> header file provides a range of macros (Q_CC_*) that are defined if the application is compiled using the specified compiler. For example, the Q_CC_SUN macro is defined if the application is compiled using Forte Developer, or Sun Studio C++.
The purpose of these macros is to enable programmers to add compiler-specific code to their application.
Macro Documentation
Q_CC_BOR
Defined if the application is compiled using Borland/Turbo C++.
Q_CC_CDS
Defined if the application is compiled using Reliant C++.
Q_CC_CLANG
Defined if the application is compiled using Clang.
Q_CC_COMEAU
Defined if the application is compiled using Comeau C++.
Q_CC_DEC
Defined if the application is compiled using DEC C++.
Q_CC_EDG
Defined if the application is compiled using Edison Design Group C++.
Q_CC_GHS
Defined if the application is compiled using Green Hills Optimizing C++ Compilers.
Q_CC_GNU
Defined if the application is compiled using GNU Compiler Collection (GCC).
Q_CC_HIGHC
Defined if the application is compiled using MetaWare High C/C++.
Q_CC_HPACC
Defined if the application is compiled using HP aC++.
Q_CC_INTEL
\obsolete
This macro used to be defined if the application was compiled with the old Intel C++ compiler for Linux, macOS or Windows. The new oneAPI C++ compiler is just a build of Clang and therefore does not define this macro.
See also Q_CC_CLANG.
Q_CC_KAI
Defined if the application is compiled using KAI C++.
Q_CC_MIPS
Defined if the application is compiled using MIPSpro C++.
Q_CC_MSVC
Defined if the application is compiled using Microsoft Visual C/C++, Intel C++ for Windows.
Q_CC_OC
Defined if the application is compiled using CenterLine C++.
Q_CC_PGI
Defined if the application is compiled using Portland Group C++.
Q_CC_SUN
Defined if the application is compiled using Forte Developer, or Sun Studio C++.
Q_CC_SYM
Defined if the application is compiled using Digital Mars C/C++ (used to be Symantec C++).
Q_CC_USLC
Defined if the application is compiled using SCO OUDK and UDK.
Q_CC_WAT
Defined if the application is compiled using Watcom C++.
Q_CONSTINIT
\since
6.4
Enforces constant initialization when supported by the compiler.
If the compiler supports the C++20 constinit
keyword, Clang's [[clang::require_constant_initialization]]
or GCC's __constinit
, then this macro expands to the first one of these that is available, otherwise it expands to nothing.
Variables marked as constinit
cause a compile-error if their initialization would have to be performed at runtime.
Note: Constant-initialized variables may still have load-time impact if they have non-trivial destruction.
For constants, you can use constexpr
since C++11, but constexpr
makes variables const
, too, whereas constinit
ensures constant initialization, but doesn't make the variable const
:
Keyword | Added | immutable | constant-initialized |
---|---|---|---|
const | C++98 | yes | not required |
constexpr | C++11 | yes | required |
constinit | C++20 | no | required |
Q_DECL_CONSTEXPR
\deprecated
[6.4] Use the constexpr
keyword instead.
This macro can be used to declare variable that should be constructed at compile-time, or an inline function that can be computed at compile-time.
See also Q_DECL_RELAXED_CONSTEXPR.
Q_DECL_EXPORT
This macro marks a symbol for shared library export (see Creating Shared Libraries).
See also Q_DECL_IMPORT.
Q_DECL_FINAL
\since
5.0 \deprecated
This macro can be used to declare an overriding virtual or a class as "final", with Java semantics. Further-derived classes can then no longer override this virtual function, or inherit from this class, respectively.
It expands to "final".
The macro goes at the end of the function, usually after the const
, if any:
// more-derived classes no longer permitted to override this: virtual void MyWidget::paintEvent(QPaintEvent*) final;
For classes, it goes in front of the :
in the class definition, if any:
class QRect final { // cannot be derived from // ... };
See also Q_DECL_OVERRIDE.
Q_DECL_IMPORT
This macro declares a symbol to be an import from a shared library (see Creating Shared Libraries).
See also Q_DECL_EXPORT.
Q_DECL_NOEXCEPT
\since
5.0 \deprecated
[6.4] Use the noexcept
keyword instead.
This macro marks a function as never throwing. If the function does nevertheless throw, the behavior is defined: std::terminate() is called.
See also Q_DECL_NOTHROW and Q_DECL_NOEXCEPT_EXPR().
Q_DECL_NOEXCEPT_EXPR(x)
\since
5.0 \deprecated
[6.4] Use the noexcept
keyword instead.
This macro marks a function as non-throwing if x is true
. If the function does nevertheless throw, the behavior is defined: std::terminate() is called.
See also Q_DECL_NOTHROW and Q_DECL_NOEXCEPT.
Q_DECL_NOTHROW
\since
5.0 \deprecated
[6.4] Use the noexcept
keyword instead.
This macro marks a function as never throwing, under no circumstances. If the function does nevertheless throw, the behavior is undefined.
See also Q_DECL_NOEXCEPT and Q_DECL_NOEXCEPT_EXPR().
Q_DECL_OVERRIDE
\since
5.0 \deprecated
This macro can be used to declare an overriding virtual function. Use of this markup will allow the compiler to generate an error if the overriding virtual function does not in fact override anything.
It expands to "override".
The macro goes at the end of the function, usually after the const
, if any:
// generate error if this doesn't actually override anything: virtual void MyWidget::paintEvent(QPaintEvent*) override;
See also Q_DECL_FINAL.
Q_DECL_RELAXED_CONSTEXPR
\deprecated
[6.4] Use the constexpr
keyword instead.
This macro can be used to declare an inline function that can be computed at compile-time according to the relaxed rules from C++14.
See also Q_DECL_CONSTEXPR.
void Q_FALLTHROUGH
\since
5.8
Can be used in switch statements at the end of case block to tell the compiler and other developers that the lack of a break statement is intentional.
This is useful since a missing break statement is often a bug, and some compilers can be configured to emit warnings when one is not found.
See also Q_UNREACHABLE() and Q_UNREACHABLE_RETURN().
const char*Q_FUNC_INFO
Expands to a string that describe the function the macro resides in. How this string looks more specifically is compiler dependent. With GNU GCC it is typically the function signature, while with other compilers it might be the line and column number.
Q_FUNC_INFO can be conveniently used with qDebug(). For example, this function:
template<typename TInputType> const TInputType &myMin(const TInputType &value1, const TInputType &value2) { qDebug() << Q_FUNC_INFO << "was called with value1:" << value1 << "value2:" << value2; if(value1 < value2) return value1; else return value2; }
when instantiated with the integer type, will with the GCC compiler produce:
const TInputType& myMin(const TInputType&, const TInputType&) [with TInputType = int] was called with value1: 3 value2: 4
If this macro is used outside a function, the behavior is undefined.
Q_LIKELY(expr)
\since
4.8
Hints to the compiler that the enclosed condition, expr, is likely to evaluate to true
.
Use of this macro can help the compiler to optimize the code.
Example:
// the condition inside the "if" will be successful most of the times for (int i = 1; i <= 365; i++) { if (Q_LIKELY(isWorkingDay(i))) { ... } ... }
See also Q_UNLIKELY().
Q_NODISCARD_CTOR
\since
6.6
Expands to [[nodiscard]]
on compilers that accept it on constructors.
Otherwise it expands to nothing.
Constructors marked as Q_NODISCARD_CTOR cause a compiler warning if a call site doesn't use the resulting object.
This macro is exists solely to prevent warnings on compilers that don't implement the feature. If your supported platforms all allow [[nodiscard]]
on constructors, we strongly recommend you use the C++ attribute directly instead of this macro.
Q_UNLIKELY(expr)
\since
4.8
Hints to the compiler that the enclosed condition, expr, is likely to evaluate to false
.
Use of this macro can help the compiler to optimize the code.
Example:
bool readConfiguration(const QFile &file) { // We expect to be asked to read an existing file if (Q_UNLIKELY(!file.exists())) { qWarning() << "File not found"; return false; } ... return true; }
See also Q_LIKELY().