If I have some class type that manages, for example, a resource, and my class needs to define a swap()
function as a part of its interface that works on objects of that type, then I usually do:
struct Foo{};
Foo f1, f2;
void swap(Foo& lhs, Foo& rhs){
// code to swap member data of lhs and rhs
}
int main(){
using std::swap;
Foo f1, f2;
swap(f1, f2);
}
Now, am I overloading
std::swap
, or specializing it?I have learned that if I want to specialize a function/class template of the standard library, then I should open the namespace
std
and declare the specialization there. e.g:
namespace std{
void swap(Foo&, Foo&);
}
- I remember that when specializing
std::hash
for my types that I intend to using as the element type to unordered associative containers, likestd::unordered_map
, I do specializestd::hash
this way:
namespace std{ // opening namespace std
template<>
class hash<Foo>{
//...
};
}
So, is this correct? Should I overload or specialize std::swap
?