<QtCompilerDetection> - Compiler-specific Macro Definitions

The <QtCompilerDetection> header file includes various compiler-specific macros. More...

Header: #include <QtCompilerDetection>

Macros

Detailed Description

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

[since 6.7] Q_NODISCARD_CTOR_X(message)

[since 6.7] Q_NODISCARD_X(message)

Expand to [[nodiscard(message)]] on compilers that support the feature.

Otherwise they expand to [[nodiscard]] and Q_NODISCARD_CTOR, respectively.

This function was introduced in Qt 6.7.

See also Q_NODISCARD_CTOR.

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_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++.

[since 6.4] Q_CONSTINIT

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:

KeywordAddedimmutableconstant-initialized
constC++98yesnot required
constexprC++11yesrequired
constinitC++20norequired

This macro was introduced in Qt 6.4.

Q_DECL_EXPORT

This macro marks a symbol for shared library export (see Creating Shared Libraries).

See also Q_DECL_IMPORT.

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.

void Q_FALLTHROUGH

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)

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().

[since 6.6] Q_NODISCARD_CTOR

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.

This macro was introduced in Qt 6.6.

See also Q_NODISCARD_CTOR_X.

[since 6.7] Q_NODISCARD_CTOR_X(message)

This macro was introduced in Qt 6.7.

[since 6.7] Q_NODISCARD_X(message)

This macro was introduced in Qt 6.7.

Q_UNLIKELY(expr)

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().