Boost logo

Boost Users :

Subject: Re: [Boost-users] Generically comparing structs
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2009-08-27 20:10:46


AMDG

Bill Buklis wrote:
> Thanks very much for taking the time for that adaptation. Unfortunately a
> lot of these strings are actually char arrays rather than pointers (way too
> much legacy code left). I couldn't figure out how to make
> BOOST_FUSION_ADAPT_STRUCT recognize the types (one of those array vs pointer
> differences), but then I realized it doesn't really matter for this case.
>

It seems to be fine as long as you use a typedef.

// doesn't compile
struct S
{
    char x[10];
};
BOOST_FUSION_ADAPT_STRUCT(S, (char[10], x));

// okay
typedef char x_type[10];
struct S
{
    char x[10];
};
BOOST_FUSION_ADAPT_STRUCT(S, (x_type, x));

> I can make this work using vector_tie and a simple wrapper. It's nowhere
> near as elegant, but it seems to work. Any thoughts on potential problems
> that I might be missing? See below:
>
> struct char_wrapper
> {
> const char * str;
>
> char_wrapper( const char* str ) : str(str) {}
> bool operator<( const char_wrapper& rhs ) const
> {
> return(strcmp(str, rhs.str) < 0);
> }
> };
>
> struct s
> {
> int a, b;
> char c[20];
> };
>
> bool operator<( const s& lhs, const s& rhs )
> {
> // sort in order of b,a,c
> return( boost::fusion::vector_tie(lhs.b, lhs.a, char_wrapper(lhs.c)) <
> boost::fusion::vector_tie(rhs.b, rhs.a, char_wrapper(rhs.c)) );
> }
>

You can't pass a non-const rvalue to vector_tie.

In Christ,
Steven Watanabe


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net