Re: Swappable concepts and ADL (was Re: Return versus Side-Effect)
On Apr 2, 2:09 pm, Alberto Ganesh Barbati <AlbertoBarb...@libero.it>
wrote:
Yechezkel Mett ha scritto:
Perhaps the following would be better (assuming it works!)
namespace std
{
auto concept Swappable<typename T> {
void swap(T&, T&)
{
T x(std::move(a));
a = std::move(b);
b = std::move(x);
}
};
auto concept MemberSwappable<typename T> : Swappable<T> {
void T::swap(T&);
void swap(T& lhs, T& rhs) { lhs.swap(rhs); }
};
template <Swappable T>
void swap(T&& a, T&& b)
{
Swappable<T>::swap(a, b);
}
}
I see... very nice. I'm starting to like concepts ;) and possibly I'm
also starting to learn how to use them effectively, thanks!
Please correct me if I'm wrong, but in this case swappability would be
achieved by:
1) by providing a explicit concept map for Swappable<T>
2) by T having a member function swap()
3) by having a free function swap in a namespace associated with T
4) by T being MoveConstructible and MoveAssignable
That was the idea, but after working my way through bits of N2501 I
don't think it'll work. Worse yet, sometimes it will and sometimes it
won't depending on instantiation order. If I've understood it
correctly :-) and depending on the precise meaning of the word "need".
I think the following should do the trick:
namespace std
{
auto concept Swappable<typename T> {
void swap(T& lhs, T& rhs)
{
T x(std::move(lhs));
lhs = std::move(rhs);
rhs = std::move(x);
}
};
auto concept MemberSwappable<typename T> {
void T::swap(T&);
};
template<MemberSwappable T>
concept_map Swappable<T> {
swap(T& lhs, T& rhs)
{
lhs.swap(rhs);
}
};
template <Swappable T>
void swap(T&& a, T&& b)
{
Swappable<T>::swap(a, b);
}
}
Yechezkel Mett
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]