Main Content

AUTOSAR C++14 Rule A18-1-6

All std::hash specializations for user-defined types shall have a noexcept function call operator

Since R2020a

Description

Rule Definition

All std::hash specializations for user-defined types shall have a noexcept function call operator.

Rationale

std::hash specializations provided by the standard library have a guarantee of no exceptions. If you manually create a std::hash specialization, emulate this guarantee for your specialization. Define all specializations of std::hash for your custom data types as noexcept.

Otherwise, standard library containers that use your specialization of std::hash indirectly might throw uncaught exceptions. The exceptions are not caught because the standard library containers do not provide a way to use try-catch blocks for exceptions from std::hash.

Polyspace Implementation

The checker flags specializations of the std::hash template with user defined types that do not have a noexcept specifier.

Troubleshooting

If you expect a rule violation but Polyspace® does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Check Information

Group: Language support library
Category: Required, Automated

Version History

Introduced in R2020a