Boost logo

Boost :

From: Xiaoming Shi (polyrandom_at_[hidden])
Date: 2002-08-23 03:50:46


Hi all,

As for the base_from_member template
http://www.boost.org/libs/utility/base_from_member.html the typical usage is
as follow:
----------------------------------------------------------------------------
---------------------------------------------------
class someclass
    : private boost::base_from_member<int>
    , private boost::base_from_member<long>
    , private boost::base_from_member<string,1>
    , private boost::base_from_member<string,2>
{
    // Helper typedef's
    typedef boost::base_from_member<int> pbase0_type;
    typedef boost::base_from_member<long> pbase1_type;
    typedef boost::base_from_member<string, 1> pbase2_type;
    typedef boost::base_from_member<string, 2> pbase3_type;

    void some_func()
    {
        pbase0_type::member=0;
        pbase3_type::member="aaa";
        .............
----------------------------------------------------------------------------
---------------------------------------------------

I think it might be a bit tedious to repeat so many typing and typedefs if
we just need to define member variable instead of explicitly construct them.
I think maybe we could modify it as follow:
----------------------------------------------------------------------------
---------------------------------------------------
#ifndef __68CF572F_350E_4486_9527_99D402E2EF8A__
#define __68CF572F_350E_4486_9527_99D402E2EF8A__

class base_as_member_void
{
};

#define DECLARE_CONSTRUCTOR() \
 base_as_member() \
 {} \
 template<typename P1> \
 explicit base_as_member(P1 p1):member1(x1) \
 {} \
 template<typename P1,typename P2> \
 base_as_member(P1 p1,P2 p2):member1(p1,p2) \
 {} \
    template<typename P1,typename P2,typename P3> \
 base_as_member(P1 p1,P2 p2,P3 p3):member1(p1,p2,p3) \
 {}

template<typename T1,typename T2=base_as_member_void,typename
T3=base_as_member_void,typename T4=base_as_member_void,
 typename T5=base_as_member_void,typename T6=base_as_member_void,typename
T7=base_as_member_void,typename T8=base_as_member_void>
class base_as_member
{
protected:
 DECLARE_CONSTRUCTOR()
 T1 member1;
 T2 member2;
 T3 member3;
 T4 member4;
 T5 member5;
 T6 member6;
 T7 member7;
 T8 member8;
};

template<typename T1>
class
base_as_member<T1,base_as_member_void,base_as_member_void,base_as_member_voi
d,

base_as_member_void,base_as_member_void,base_as_member_void,base_as_member_v
oid>
{
protected:
 DECLARE_CONSTRUCTOR()
 T1 member1;
};

template<typename T1,typename T2>
class base_as_member<T1,T2,base_as_member_void,base_as_member_void,

base_as_member_void,base_as_member_void,base_as_member_void,base_as_member_v
oid>
{
protected:
 DECLARE_CONSTRUCTOR()
 T1 member1;
 T2 member2;
};

// lengthy declarations omitted for simplicity

#endif //__68CF572F_350E_4486_9527_99D402E2EF8A__
----------------------------------------------------------------------------
---------------------------------------------------
so you could use it as:
class someclass
    : private base_as_member<int,long,string,string>
{
    void some_func()
    {
        mMember1=0;
        mMember4="aaa";

As far as i can see, the shortcoming of it ( if compares with the original
base_from_member ) is that it cannot work well in compilers that doesn't
support partial template initialization.


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