Boost logo

Boost :

From: Matias Capeletto (matias.capeletto_at_[hidden])
Date: 2007-02-26 13:13:07


On 2/26/07, Thorsten Ottosen <thorsten.ottosen_at_[hidden]> wrote:
> Matias Capeletto wrote:
> > In 2/26/07, Thorsten Ottosen <thorsten.ottosen_at_[hidden]> wrote:
> >
> >>Matias Capeletto wrote:
>
> >>>I think I can change the current implementation to avoid the first
> >>>conversion if the compiler support the mutant_relation (you can read
> >>>about this in the rationale), because a reinterpret_cast can be used
> >>>to convert between bimap pairs and relation.
> >>
> >>I'm somewhat uncomfortable about this casting. Is it provable legal by
> >>the standard? (What works fine for pair<int,int>, might crash for
> >>pair<Foo,Bar>.)
> >
> >
> > Yep... the reinterpret cast works in almost every actual compiler. It
> > uses a non standard feature. It assumes that:
> >
> > struct Pair_AB { TA first; TB second; };
> >
> > have the same structure that
> >
> > struct Pair_BA { TB first; TA second; };
> >
> > In the standard this is true if TA and TB are POD types.
> > But in the real world this is generally implemented this way for non
> > POD types too.
>
> I'm certainly confused here. What do you mean by "same structure"? If
> you had written
>
> struct Pair_BA { TA first; TB second; };
>
> I might have understood it.

I was referring to the same storage layout. The correct structures are:

struct Pair_AB { TA first; TB second; };
struct Pair_BA { TA second; TB first; };

> > In boost/bimap/relation/relation.cpp the library checks if it can use
> > the mutant_relation for the types in question and if not it uses a
> > standard_relation, that is implemented using references to the types.
>
> Right. How it sucks that std::pair did not go for reference returning
> functions instead of members!.

Yes, life will be easier.

> What if you make the members of the view pairs something like
>
> template< class Pair& >
> struct get_first
> {
> Pair& p_;
> operator typename Pair::first_type&() const
> { return p_.first; }
> };
>
> ?

It is a possible way to achieve the same results. The problem with it
is that the size of the resulting pair will be two references bigger.
This option was analyze and discarded for that reason.

Regards
Matias


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