I have the following code.
#include <iostream>
#include <unordered_map>
struct Object {
~Object() noexcept = default;
Object() noexcept = default;
Object(const Object&) = delete;
Object(Object&&) noexcept = default;
Object& operator=(const Object&) = delete;
Object& operator=(Object&&) noexcept = default;
std::unordered_map<int, int> map{};
};
Object get_object() {
Object o{};
o.map[0] = 1;
return o; // error
}
int main() {
Object o = get_object();
std::cout << o.map[0] << '\n';
return 0;
}
The static analysis in Visual Studio 2022 shows the following error.
Line 18: function "Object::Object(const Object &)" (declared at line 7) cannot be referenced -- it is a deleted function
However, the code compiles just fine and the result is 1
as expected.
What actually happens? Can std::unordered_map
be really moved? If this isn't a problem, how can I tell the static analysis that this is a false positive?