Qt-Autosar-A17.6.1

Non-standard entities shall not be added to standard namespaces

Required inputs: IR

Rationale

Adding declarations or definitions to namespace std or its sub-namespaces, or to namespace posix or its sub-namespaces leads to undefined behavior.

Declaring an explicit specialization of a member function or member function template of a standard library class or class template leads to undefined behavior.

Declaring an explicit or partial specialization of a member class template of a standard library class or class template leads to undefined behavior.

Exceptions

It is allowed by the language standard to add a specialization to namespace std if the declaration depends on a user-defined type, meets the requirements for the original template and is not explicitly forbidden.

It is allowed by the language standard to explicitly instantiate a template defined in the standard library if the declaration depends on a user defined type and meets the requirements for the original template.

Example
// $Id: A17-6-1.cpp 305588 2018-01-29 11:07:35Z michal.szczepankiewicz $
    #include <cstdint>
    #include <limits>
    #include <memory>
    #include <type_traits>
    #include <utility>

    namespace std
    {

    // Non-compliant - An alias definition is added to namespace std.
    // This is a compile error in C++17, since std::byte is already defined.
    using byte = std::uint8_t;

    // Non-compliant - A function definition added to namespace std.
    pair<int, int> operator+(pair<int, int> const& x, pair<int, int> const& y)
    {
        return pair<int, int>(x.first + y.first, x.second + y.second);
    }

    }    // namespace std

    struct MyType
    {
        int value;
    };

    namespace std
    {

    // Non-compliant - std::numeric_limits may not be specialized for
    // non-arithmetic types [limits.numeric].
    template <>
    struct numeric_limits<MyType> : numeric_limits<int>
    {
    };

    // Non-compliant - Structures in <type_traits>, except for std::common_type,
    // may not be specialized [meta.type.synop].
    template <>
    struct is_arithmetic<MyType> : true_type
    {
    };

    // Compliant - std::hash may be specialized for a user type if the
    // specialization fulfills the requirements in [unord.hash].
    template <>
    struct hash<MyType>
    {
        using result_type = size_t;    // deprecated in C++17
        using argument_type = MyType; // deprecated in C++17

          size_t operator()(MyType const& x) const noexcept
          {
              return hash<int>()(x.value);
          }
    };

    }    // namespace std
See Also
  • SEI CERT C++ Coding Standard [10]: DCL58-CPP: Do not modify the standard namespaces
  • C++ Core Guidelines [11]: SL.3: Do not add non-standard entities to namespace std
  • ISO/IEC 14882:2014 [3]: 17.6.4.2: [namespace.constraints]
  • ISO/IEC 14882:2014 [3]: 18.3.2.1: [limits.numeric]
  • ISO/IEC 14882:2014 [3]: 20.9.13: [unord.hash]
  • ISO/IEC 14882:2014 [3]: 20.10.2: [meta.type.synop]

Possible Messages

Key

Text

Severity

Disabled

std_extension

Invalid addition to std namespace

None

False

std_specialization

Invalid std template specialization

None

False

Options

std_specialization_blacklist

std_specialization_blacklist

Type: set[bauhaus.analysis.config.QualifiedName]

Default: {'std::add_const', 'std::add_cv', 'std::add_lvalue_reference', 'std::add_pointer', 'std::add_rvalue_reference', 'std::add_volatile', 'std::aligned_storage', 'std::aligned_union', 'std::alignment_of', 'std::binary_function', 'std::common_type', 'std::conditional', 'std::conjunction', 'std::decay', 'std::disjunction', 'std::enable_if', 'std::endian', 'std::extent', 'std::has_unique_object_representations', 'std::has_virtual_destructor', 'std::integral_constant', 'std::invoke_result', 'std::is_abstract', 'std::is_aggregate', 'std::is_arithmetic', 'std::is_array', 'std::is_assignable', 'std::is_base_of', 'std::is_class', 'std::is_compound', 'std::is_const', 'std::is_constructible', 'std::is_convertible', 'std::is_copy_assignable', 'std::is_copy_constructible', 'std::is_default_constructible', 'std::is_destructible', 'std::is_empty', 'std::is_enum', 'std::is_final', 'std::is_floating_point', 'std::is_function', 'std::is_fundamental', 'std::is_integral', 'std::is_invocable', 'std::is_invocable_r', 'std::is_literal_type', 'std::is_lvalue_reference', 'std::is_member_function_pointer', 'std::is_member_object_pointer', 'std::is_member_pointer', 'std::is_move_assignable', 'std::is_move_constructible', 'std::is_nothrow_assignable', 'std::is_nothrow_constructible', 'std::is_nothrow_convertible', 'std::is_nothrow_copy_assignable', 'std::is_nothrow_copy_constructible', 'std::is_nothrow_default_constructible', 'std::is_nothrow_destructible', 'std::is_nothrow_invocable', 'std::is_nothrow_invocable_r', 'std::is_nothrow_move_assignable', 'std::is_nothrow_move_constructible', 'std::is_nothrow_swappable', 'std::is_nothrow_swappable_with', 'std::is_null_pointer', 'std::is_object', 'std::is_pod', 'std::is_pointer', 'std::is_polymorphic', 'std::is_reference', 'std::is_rvalue_reference', 'std::is_same', 'std::is_scalar', 'std::is_signed', 'std::is_standard_layout', 'std::is_swappable', 'std::is_swappable_with', 'std::is_trivial', 'std::is_trivially_assignable', 'std::is_trivially_constructible', 'std::is_trivially_copy_assignable', 'std::is_trivially_copy_constructible', 'std::is_trivially_copyable', 'std::is_trivially_default_constructible', 'std::is_trivially_destructible', 'std::is_trivially_move_assignable', 'std::is_trivially_move_constructible', 'std::is_union', 'std::is_unsigned', 'std::is_void', 'std::is_volatile', 'std::make_signed', 'std::make_unsigned', 'std::negation', 'std::rank', 'std::remove_all_extents', 'std::remove_const', 'std::remove_cv', 'std::remove_cvref', 'std::remove_extent', 'std::remove_pointer', 'std::remove_reference', 'std::remove_volatile', 'std::result_of', 'std::unary_function', 'std::underlying_type', 'std::void_t'}

Templates in namespace std that must not be specialized.
 

std_specialization_whitelist

std_specialization_whitelist : set[bauhaus.analysis.config.QualifiedName] = {'std::common_type'}

Templates in namespace std that can be specialized (even when found on the blacklist).