Boost logo

Boost :

From: Eric Friedman (ebf_at_[hidden])
Date: 2002-07-21 22:58:37


See my attached implementation of extract.hpp (the current version of what
is being used in Boost.Variant).

Simplified implementation (i.e. without const considerations, workarounds,
etc.):

    template <typename Holder>
    struct extract_traits;

    template <typename T, typename Holder>
    T* extract(Holder* operand)
    {
        if (!operand) return static_cast<T*>(0);
        return extract_traits<Holder>::template
extract_pointer<T>(*operand);
    }

    template <typename T, typename Holder>
    T& extract(Holder& operand)
    {
        T* result = extract_traits<Holder>::template
extract_pointer<T>(operand);
        if (!result) throw bad_extract();
        return *result;
    }

---
This approach:
 * Is extensible (via extract_traits specializations).
 * Is similar to any_cast and dynamic_cast (in terms of pointer vs.
reference semantics).
 * Centralizes extract's behavior (though workarounds for lack of partial
specialization undermines this to some extent). Only
extract_traits<Holder>::extract_pointer must be implemented for a given
Holder, for which no null-pointer tests are needed.
Let me know what you think.
Eric
"David Abrahams" <david.abrahams_at_[hidden]> wrote in message
news:141701c230cc$76960500$6501a8c0_at_boostconsulting.com...
>
> From: "Itay Maman" <itay_maman_at_[hidden]>
>
> > > I realize that poses some problems for unification, since it's not
> > possible
> > > to non-intrusively customize constructors for a single class template.
> I'm
> > > willing to discuss changes, if neccessary.
> > >
> > > -Dave
> > >
> >
> > (i) Is it in the CVS?
>
> Yes: boost/python/extract.hpp
>
> > (ii) There's this MSVC namespace-related bug, which makes it impossible
> to
> > use extract<T>(x). The workaround I have used so far, is to pass T as a
> > dummy parameter, i.e: extract(x, boost::type<T>());
>
> The class formulation doesn't suffer from that, but there's also a
> workaround for the function formulation...
>
> > I wonder if there are other workarounds which will not affect the
> interface
> > of the function?
>
>
>   template <class T, class U>
>   whatever extract(variant<U> const&, boost::type<T>* = 0);
>
> -Dave
>
>
> _______________________________________________
> Unsubscribe & other changes:
http://lists.boost.org/mailman/listinfo.cgi/boost
>
begin 666 extract.hpp
M+R\M+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2T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Q(
M;VQD97(^.CIT96UP;&%T92!E>'1R86-T7W!O:6YT97(\5#XH*F]P97)A;F0I
M.PT*(" @('T-"@T*<'5B;&EC._at_T*"71E;7!L871E(#QT>7!E;F%M92!(;VQD
M97(^#0H)<W1A=&EC(%0J(&5X96-U=&4H2&]L9&5R*B!O<&5R86YD*0T*"7L-
M"@D)<F5T=7)N(&5X96-U=&5?:6UP;"AO<&5R86YD+"!M<&PZ.F)O;VQ?8SQM
M<&PZ.FES7V-O;G-T/$AO;&1E<CXZ.G9A;'5E/B_at_I*3L-"@E]#0I].PT*#0IT
M96UP;&%T92 \='EP96YA;64_at_5"P_at_8F]O;"!I<U]C;VYS=#X-"G-T<G5C="!E
M>'1R86-T7W)E9F5R96YC95]I;7!L#0I[#0IP<FEV871E._at_T*"71E;7!L871E
M(#QT>7!E;F%M92!(;VQD97(^#0H)<W1A=&EC(%0F(&5X96-U=&5?:6UP;"A(
M;VQD97(F(&]P97)A;F0L(&UP;#HZ9F%L<V5?8RD-"B @("![#0H)"50J(')E
M<W5L=" ](&5X=')A8W1?=')A:71S/$AO;&1E<CXZ.G1E;7!L871E(&5X=')A
M8W1?<&]I;G1E<CQ4/BAO<&5R86YD*3L-"B @(" @(" @:68@*"%R97-U;'0I
M#0H@(" @(" @(" @("!T:')O=R!B861?97AT<F%C="@I.PT*(" @(" @("!R
M971U<FX@*G)E<W5L=#L-"B @("!]#0H-"@ET96UP;&%T92 \='EP96YA;64@
M2&]L9&5R/@T*"7-T871I8R!4)B!E>&5C=71E7VEM<&PH8V]N<W0_at_2&]L9&5R
M)B!O<&5R86YD+"!M<&PZ.G1R=65?8RD-"B @("![#0H)"50J(')E<W5L=" ]
M(&5X=')A8W1?=')A:71S/$AO;&1E<CXZ.G1E;7!L871E(&5X=')A8W1?<&]I
M;G1E<CQ4/BAO<&5R86YD*3L-"B @(" @(" @:68@*"%R97-U;'0I#0H@(" @
M(" @(" @("!T:')O=R!B861?97AT<F%C="@I.PT*(" @(" @("!R971U<FX@
M*G)E<W5L=#L-"B @("!]#0H-"G!U8FQI8SH-"@ET96UP;&%T92 \='EP96YA
M;64_at_2&]L9&5R/@T*(" @('-T871I8R!4)B!E>&5C=71E*$AO;&1E<B8@;W!E
M<F%N9"D-"B @("![#0H)"7)E='5R;B!E>&5C=71E7VEM<&PH;W!E<F%N9"P@
M;7!L.CIB;V]L7V,\;7!L.CII<U]C;VYS=#Q(;VQD97(^.CIV86QU93XH*2D[
M#0H@(" @?0T*?3L-"@T*?2 O+R!N86UE<W!A8V4_at_9&5T86EL#0H-"G1E;7!L
M871E(#QT>7!E;F%M92!4+"!T>7!E;F%M92!(;VQD97(^#0H@(" @='EP96YA
M;64@;7!L.CIA<'!L>5]I9CP-"B @(" @(" @("!M<&PZ.FES7W!O:6YT97(\
M2&]L9&5R/@T*(" @(" @(" L(&UP;#HZ861D7W!O:6YT97(\5#X-"B @(" @
M(" @+"!M<&PZ.F%D9%]R969E<F5N8V4\5#X-"B @(" @(" @/CHZ='EP90T*
M97AT<F%C="A(;VQD97(F(&]P97)A;F0I#0I[#0H@(" @='EP961E9B!T>7!E
M;F%M92!M<&PZ.G-E;&5C=%]I9CP-"B @(" @(" @("!M<&PZ.FES7W!O:6YT
M97(\2&]L9&5R/@T*(" @(" @(" L(&1E=&%I;#HZ97AT<F%C=%]P;VEN=&5R
M7VEM<&P\5#X-"@D)+"!D971A:6PZ.F5X=')A8W1?<F5F97)E;F-E7VEM<&P\
M5"P@;7!L.CII<U]C;VYS=#Q(;VQD97(^.CIV86QU93X-"B @(" @(" @/CHZ
M='EP92!I;7!L.PT*#0H@(" @<F5T=7)N(&EM<&PZ.F5X96-U=&4H;W!E<F%N
M9"D[#0I]#0H-"GT@+R\@;F%M97-P86-E(&)O;W-T#0H-"B-E;F1I9B O+R!"
23T]35%]%6%1204-47TA04 T*
`
end

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