Reputation: 4454
Given,
auto a = make_pair(1,"one");
auto b = make_pair(2,"two");
a.swap(b);
When do I really need to use a.swap(b)
when there is a more generalized std::swap(a,b)
that works the same way.
Upvotes: 2
Views: 793
Reputation: 596256
std::swap(a, b)
is specialized for std::pair
to call a.swap(b)
internally. std::swap()
has many container-specific specializations so it can use container-appropriate methods for the actual swapping.
If you are writing code for a specific type, consider using a.swap(b)
directly (or whatever method is appropriate for that type).
If you are writing code that is generic for multiple types, use std::swap()
instead, and let the compiler work out which specialization(s) to call.
Upvotes: 7