V1061. Extending 'std' or 'posix' namespace may result in undefined behavior.
The analyzer has detected an extension of the 'std' or 'posix' namespace. Even though such a program compiles and runs successfully, modifying namespaces' data may result in undefined behavior if the standard does not state otherwise.
The contents of the 'std' namespace is defined solely by the standardization committee, and the standard prohibits adding the following to it:
- variable declarations;
- function declarations;
- class/structure/union declarations;
- enumeration declarations;
- function/class/variable template declarations (C++14);
The standard does allow adding the following specializations of templates defined in the 'std' namespace given that they depend on at least one program-defined type:
- full or partial specialization of a class template;
- full specialization of a function template (up to C++20);
- full or partial specialization of a variable template not located in the '<type_traits>' header (up to C++20);
However, specializations of templates located inside classes or class templates are prohibited.
The most common scenarios when the user extends the 'std' namespace are adding an overload of the 'std::swap' function and adding a full/partial specialization of the 'std::hash' class template.
The following example illustrates adding an overload of the 'std::swap' function:
template <typename T>
class MyTemplateClass
{
....
};
class MyClass
{
....
};
namespace std
{
template <typename T>
void swap(MyTemplateClass<T> &a, MyTemplateClass<T> &b) noexcept // UB
{
....
}
template <>
void swap(MyClass &a, MyClass &b) noexcept // UB since C++20
{
....
};
}
The first function template is not a specialization of 'std::swap', so a declaration like that will lead to undefined behavior. The second function template is a specialization, and the program's behavior is defined up to the C++20 standard. However, there is another way: we could move both functions out of the 'std' namespace and place them to the one where classes are defined:
template <typename T>
class MyTemplateClass
{
....
};
class MyClass
{
....
};
template <typename T>
void swap(MyTemplateClass<T> &a, MyTemplateClass<T> &b) noexcept
{
....
}
void swap(MyClass &a, MyClass &b) noexcept
{
....
};
Now, when you need to write a function template that uses the swap function on two objects of type T, you can write the following:
template <typename T>
void MyFunction(T& obj1, T& obj2)
{
using std::swap; // make std::swap visible for overload resolution
....
swap(obj1, obj2); // best match of 'swap' for objects of type T
....
}
Now the compiler will select the required overload based on argument-dependent lookup (ADL): the user-defined 'swap' functions for the 'MyClass' class and for the 'MyTemplateClass' class template, and the standard 'std::swap' function for all other types.
The next example demonstrates adding a specialization of the class template 'std::hash':
namespace Foo
{
class Bar
{
....
};
}
namespace std
{
template <>
struct hash<Foo::Bar>
{
size_t operator()(const Foo::Bar &) const noexcept;
};
}
From the standard's point of view, this code is valid, and so the analyzer does not issue the warning here. But starting with C++11, there is also another way to do this, namely by writing the class template specialization outside the 'std' namespace:
template <>
struct std::hash<Foo::Bar>
{
size_t operator()(const Foo::Bar &) const noexcept;
};
Unlike the 'std' namespace, the C++ standard prohibits any modification of the 'posix' namespace at all:
namespace posix
{
int x; // UB
}
More detail here:
- C++17 (working draft N4659), 20.5.4.2.1
- C++20 (working draft N4860), 16.5.4.2.1
This diagnostic is classified as:
|
You can look at examples of errors detected by the V1061 diagnostic. |