69 lines · plain
1.. title:: clang-tidy - bugprone-std-namespace-modification2 3bugprone-std-namespace-modification4===================================5 6Warns on modifications of the ``std`` or ``posix`` namespaces which can7result in undefined behavior.8 9The ``std`` (or ``posix``) namespace is allowed to be extended with (class or10function) template specializations that depend on an user-defined type (a type11that is not defined in the standard system headers).12 13The check detects the following (user provided) declarations in namespace14``std`` or ``posix``:15 16- Anything that is not a template specialization.17- Explicit specializations of any standard library function template or class18 template, if it does not have any user-defined type as template argument.19- Explicit specializations of any member function of a standard library class20 template.21- Explicit specializations of any member function template of a standard22 library class or class template.23- Explicit or partial specialization of any member class template of a standard24 library class or class template.25 26Examples:27 28.. code-block:: c++29 30 namespace std {31 int x; // warning: modification of 'std' namespace can result in undefined behavior [bugprone-dont-modify-std-namespace]32 }33 34 namespace posix::a { // warning: modification of 'posix' namespace can result in undefined behavior35 }36 37 template <>38 struct ::std::hash<long> { // warning: modification of 'std' namespace can result in undefined behavior39 unsigned long operator()(const long &K) const {40 return K;41 }42 };43 44 struct MyData { long data; };45 46 template <>47 struct ::std::hash<MyData> { // no warning: specialization with user-defined type48 unsigned long operator()(const MyData &K) const {49 return K.data;50 }51 };52 53 namespace std {54 template <>55 void swap<bool>(bool &a, bool &b); // warning: modification of 'std' namespace can result in undefined behavior56 57 template <>58 bool less<void>::operator()<MyData &&, MyData &&>(MyData &&, MyData &&) const { // warning: modification of 'std' namespace can result in undefined behavior59 return true;60 }61 }62 63References64----------65 66This check corresponds to the CERT C++ Coding Standard rule67`DCL58-CPP. Do not modify the standard namespaces68<https://www.securecoding.cert.org/confluence/display/cplusplus/DCL58-CPP.+Do+not+modify+the+standard+namespaces>`_.69