Boost logo

Boost :

Subject: Re: [boost] [1.43.0] [variant] Compilation errors when swapping variant type objects
From: Jeffrey Lee Hellrung, Jr. (jhellrung_at_[hidden])
Date: 2010-09-29 02:50:41


On 9/28/2010 10:50 PM, Adam Badura wrote:
> We are using Boost 1.43.0 (we tried to move to 1.44.0 but there were
> some problems which I reported some time ago) in Visual Studio 2010.
> And there are some compilation problems when variant type objects are
> swapped. This does not affect all variant types but I didn't figured out
> what are the exact conditions (however I guess it is likely something
> with namespaces and templates).
>
> Following code demonstrates the issue:
> ----------------------------------------
> #include <cstdlib>
> #include <list>
> #include <boost/variant.hpp>
>
>
> int main()
> {
> typedef boost::variant< std::list< int >::iterator > variant_type;
>
> variant_type v;
> variant_type().swap( v );
>
> return EXIT_SUCCESS;
> }
> ----------------------------------------
>
> compilation fails with following message:
> ----------------------------------------
> 1>d:\libraries\boost_1_43_0\boost\variant\detail\move.hpp(155): error
> C2668: 'boost::detail::variant::detail::move_swap::swap' : ambiguous
> call to overloaded function
> 1> d:\libraries\boost_1_43_0\boost\variant\detail\move.hpp(141): could
> be 'void boost::detail::variant::detail::move_swap::swap<T>(T &,T &)'
> 1> with
> 1> [
> 1> T=T0
> 1> ]
> 1> c:\program files\microsoft visual studio
> 10.0\vc\include\utility(100): or 'void std::swap<T>(_Ty &,_Ty &)' [found
> using argument-dependent lookup]
> 1> with
> 1> [
> 1> T=T0,
> 1> _Ty=T0
> 1> ]
> 1> while trying to match the argument list '(T0, T0)'
[...]

Local changes you can make to get a quick fix (I hope): Try expressing
the implementation of boost::detail::move_swap in terms of 2 independent
template parameters, rather than 1:

template <typename T,typename U>
inline void swap(T& lhs, U& rhs)
{
     T tmp( boost::detail::variant::move(lhs) );
     lhs = boost::detail::variant::move(rhs);
     rhs = boost::detail::variant::move(tmp);
}

This is the same "trick" that boost::swap uses to avoid ambiguity with
std::swap, I believe; see the comment in boost/utility/swap.hpp.

I verified that this fixes your issue on MSVC9, at least.

I don't have boost 1.44 installed (yet), but you might want to check if
it looks like the same problem exists there, and if it does, file a trac
ticket...?

- Jeff


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