AutosarC++19_03-A16.6.1

#error directive shall not be used

Required inputs: IR

Rationale

Using the pre-processor #error directive may lead to code that is complicated and not clear for developers. The #error directive can not be applied to templates as it will not be evaluated as a per-instance template deduction.

Static assertion, similarly to #error directive, provides a compile-time error checking.

However, static_assert behaves correctly in all C++ concepts and makes the code more readable and does not rely on pre-processor directives.

Note: “#error” is anyway not allowed, see A16-0-1. This rule is kept in case A16-0-1 is disabled in a project.

Example
// $Id: A16-6-1.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
    #include <cstdint>
    #include <type_traits>
    constexpr std::int32_t value = 0;
    #if value > 10
    #error "Incorrect value" // Non-compliant
    #endif
    void F1() noexcept
    {
        static_assert(value <= 10, "Incorrect value"); // Compliant
        // ...
    }
    template <typename T>
    void F2(T& a)
    {
        static_assert(std::is_copy_constructible<T>::value,
                       "f2() function requires copying"); // Compliant
        // ...
    }
See Also
  • none

Possible Messages

Key

Text

Severity

Disabled

error

Use of #error

None

False

Options