Boost logo

Boost :

Subject: Re: [boost] iterator_category> algorithm selection with cast operator
From: David Abrahams (dave_at_[hidden])
Date: 2010-06-25 20:47:52


At Fri, 25 Jun 2010 08:11:28 -0700 (PDT),
gzp wrote:
>
> My first idea was to use multiple inheritance, but it result error
> (ambiguous call)
> struct A {};
> struct B : A {};
> struct A2 : A {};
> struct B2 : A2, B {};
>
> void foo( A ) { alg1... }
> void foo( B ) { alg2... specialized for B }
> void foo( A2 ) { alg3... specialized for A2 }
> void g() { B2 b; f(b); } // error: is it f(B) or f(A2)

<snipp>

> So my question is that, is it a working method, or the whole concept is
> wrong ?

Your best bet here is probably to use multiple _virtual_ inheritance

struct A {};
struct B : virtual A {};
struct A2 : virtual A {};
struct B2 : virtual A2, virtual B {};

You might also want to check the generated code, though. It's
conceivable (although probably not very important if so) that the
compiler won't optimize away the tag structs.

-- 
Dave Abrahams
BoostPro Computing
http://www.boostpro.com

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