tidy - bugprone-std-namespace-modification — Extra Clang Tools 22.0.0git documentation (original) (raw)

Warns on modifications of the std or posix namespaces which can result in undefined behavior.

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 namespacestd or posix:

Examples:

namespace std { int x; // warning: modification of 'std' namespace can result in undefined behavior [bugprone-dont-modify-std-namespace] }

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

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

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

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

References

This check corresponds to the CERT C++ Coding Standard ruleDCL58-CPP. Do not modify the standard namespaces.