Boost logo

Boost :

Subject: Re: [boost] [swap] ruminations
From: Jeffrey Hellrung (jhellrung_at_[hidden])
Date: 2009-11-05 10:05:43


Joseph Gauterin wrote:
> I Googled for tsoob, but couldnt' find any references to it. It would
> be helpful to have a look at tsoob::swap to see what's causing the
> ambiguity and how/if it could be resolved.
>
> Also, I always intended for boost::swap to be called explicitly,
> rather than though ADL - it's supposed to hise the use of ADL from the
> user (no need for 'using std::swap' etc). If you use a fully qualified
> call, is there still ambiguity between boost and tsoob?

Of course, tsoob is just a made-up name...

Upon further reflection, this is probably something that shouldn't
happen, as it depends on the user wanting to use their own fallback swap
function rather than std::swap. I was (however rightly or wrongly)
concerned with code that does something like the following:

-------- BEGIN CODE --------
#include <algorithm> // for std::swap

#include <boost/utility/swap.hpp>

namespace std { struct X; }
namespace boost { struct Y; }

namespace tsoob { namespace no_adl {
#if 0 // Enabling this one gives ambiguities with std::swap
template< class T >
void swap(T& x1, T& x2) { /* fallback custom generic swap */ throw 0; }
#endif
#if 0 // Enabling this one gives ambiguities with boost::swap
template< class T1, class T2 >
void swap(T1& x1, T2& x2) { /* fallback custom generic swap */ throw 0; }
#endif
} } // namespace no_adl / namespace tsoob

namespace tsoob_swap_impl {
template< class T >
void swap_impl(T& x1, T& x2)
{
     using namespace ::tsoob::no_adl;
     swap(x1, x2);
}
} // namespace tsoob_swap_impl

namespace tsoob { namespace adl {
template< class T >
void swap(T& x1, T& x2) { ::tsoob_swap_impl::swap_impl(x1, x2); }
} // namespace adl
template< class T > struct Z { };
} // namespace tsoob

int main()
{
     tsoob::Z< std::X > zx1, zx2;
     tsoob::adl::swap(zx1, zx2);
     tsoob::Z< boost::Y > zy1, zy2;
     tsoob::adl::swap(zy1, zy2);
}
-------- END CODE --------

At the end of the day, this is probably just not a Good Idea.

However, I still think it might've been a good idea to insulate
boost::swap from being found via ADL at all by inserting another
namespace in there (so it's boost::adl::swap) rather than declaring
boost::swap to have 2 template parameters to make it less preferred than
std::swap. Evidently I can't think of a good example where this is a
problem, though :/

Apologies for the noise,

- Jeff


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk