tags:

views:

193

answers:

4
typedef boost::variant<long long,double,string> possibleTypes ;

set<possibleTypes,less<possibleTypes> > ascSet ;
set<possibleTypes,greater<possibleTypes> > descSet ;

When I try to compile I get a bunch of errors in some library headers.
But, if I remove the third line (the one with descSet ) the code compile just fine.

What's the problem? Can't boost::variant objects be sorted in descendant order??

Edit:
I'm using Visual Studio 2005 and Boost 1.38.0 and the following command line:

cl /EHsc /I"C:\boost_1_38_0" test.cpp

Edit2 As it was suggested, by Doug T, if I define this:

bool operator>(const possibleTypes& a, const possibleTypes& b){
   return b < a ;
}

Then the following code doesn't compile:

possibleTypes pt1="a", pt2="b" ;
greater<possibleTypes> func ;   
cout << func(pt1,pt2) << endl ;

However, this code compiles just fine:

possibleTypes pt1="a", pt2="b" ;
cout << (pt1 > pt2) << endl ;

Could anyone help me to understand why??

I tried with VC++ 2005 and GCC 3.4.6

+3  A: 

It appears a < operator is defined for boost::variant but not a > operator. Thus perhaps std::less<> works but not std::greater<>

See here

I would try defining a a free > operator.

bool operator > (boost::variant<...> lhs, boost::variant<..> rhs)
{
    return (rhs < lhs) // thanks Chris Jester Young
}
Doug T.
Why not just: return rhs < lhs
Chris Jester-Young
yeah that would be simpler, wouldn't it :)
Doug T.
+1  A: 

Why have two sets? Why not have just one and use a reverse iterator when you need descending order?

Ferruccio
I need the ascendant order to avoid changing existing code (written by others). And the two sets are just for testing.
GetFree
+1  A: 

You need to have the greater operator available. If possibleTypes does not provide one, you can either define a free one like Doug suggested or try to use Boost operators.

lothar
A: 

As it was suggested, if I define this:

bool operator>(const possibleTypes& a, const possibleTypes& b){
   return b < a ;
}

Then the following code doesn't compile:

possibleTypes pt1="a", pt2="b" ;
greater<possibleTypes> func ;   
cout << func(pt1,pt2) << endl ;

However, this code compiles just fine:

possibleTypes pt1="a", pt2="b" ;
cout << (pt1 > pt2) << endl ;

Could anyone help me to understand why??

I tried with VC++ 2005 and GCC 3.4.6

GetFree
Max Lybbert
Nope - Argument-dependent lookup. It's not going to search everywhere, it looks in the namespace of the caller (std::greater) and the namespace of the arguments (boost::variant). In the second case, the namespace of the caller is (probably) the global namespace.
MSalters
Thanks, Max. The problem was fixed adding operator> to the boost namespace.And thanks, MSalters, for clarifying the rules.
GetFree