AutosarC++17_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 271687 2017-03-23 08:57:35Z piotr.tanski $
#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