File: dcl58-cpp.rst

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (60 lines) | stat: -rw-r--r-- 2,285 bytes parent folder | download | duplicates (15)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
.. title:: clang-tidy - cert-dcl58-cpp

cert-dcl58-cpp
==============

Modification of the ``std`` or ``posix`` namespace can result in undefined
behavior.
This check warns for such modifications.
The ``std`` (or ``posix``) namespace is allowed to be extended with (class or
function) template specializations that depend on an user-defined type (a type
that is not defined in the standard system headers).

The check detects the following (user provided) declarations in namespace ``std`` or ``posix``:

- Anything that is not a template specialization.
- Explicit specializations of any standard library function template or class template, if it does not have any user-defined type as template argument.
- Explicit specializations of any member function of a standard library class template.
- Explicit specializations of any member function template of a standard library class or class template.
- Explicit or partial specialization of any member class template of a standard library class or class template.

Examples:

.. code-block:: c++

  namespace std {
    int x; // warning: modification of 'std' namespace can result in undefined behavior [cert-dcl58-cpp]
  }

  namespace posix::a { // warning: modification of 'posix' namespace can result in undefined behavior
  }

  template <>
  struct ::std::hash<long> { // warning: modification of 'std' namespace can result in undefined behavior
    unsigned long operator()(const long &K) const {
      return K;
    }
  };

  struct MyData { long data; };

  template <>
  struct ::std::hash<MyData> { // no warning: specialization with user-defined type
    unsigned long operator()(const MyData &K) const {
      return K.data;
    }
  };

  namespace std {
    template <>
    void swap<bool>(bool &a, bool &b); // warning: modification of 'std' namespace can result in undefined behavior

    template <>
    bool less<void>::operator()<MyData &&, MyData &&>(MyData &&, MyData &&) const { // warning: modification of 'std' namespace can result in undefined behavior
      return true;
    }
  }

This check corresponds to the CERT C++ Coding Standard rule
`DCL58-CPP. Do not modify the standard namespaces
<https://www.securecoding.cert.org/confluence/display/cplusplus/DCL58-CPP.+Do+not+modify+the+standard+namespaces>`_.