Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > templates - circular

Reply
Thread Tools

templates - circular

 
 
er
Guest
Posts: n/a
 
      03-06-2008
Hi all,

1)

I have N=2 distinct pairs of objects {(x0,y0),(x1,y1)} related as

x0 -> y0
y0 -> {x1}
x1 -> y1
y1 -> {x0}

where -> represents, for example, has-a-ptr-to. Classes of objects
labeled x# and y# may have to support some operations (to safisfy
their left or right neighbors) but are of arbitrary type. So a class
for x# has to be like template<class Ty> class M_x{}, but also
template<class Tx> class M_y{},
which runs into a circular problem.

Any suggestion, please?

2) How about N>2? for example N=3,

x0 -> y0
y0 -> {x1,x2}
x1 -> y1
y1 -> {x0,x2}
x2 -> y2
y2 -> {x0,x1}
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      03-06-2008
er wrote:
> Hi all,
>
> 1)
>
> I have N=2 distinct pairs of objects {(x0,y0),(x1,y1)} related as
>
> x0 -> y0
> y0 -> {x1}
> x1 -> y1
> y1 -> {x0}
>
> where -> represents, for example, has-a-ptr-to. Classes of objects
> labeled x# and y# may have to support some operations (to safisfy
> their left or right neighbors) but are of arbitrary type. So a class
> for x# has to be like template<class Ty> class M_x{}, but also
> template<class Tx> class M_y{},
> which runs into a circular problem.
>
> Any suggestion, please?
>
> 2) How about N>2? for example N=3,
>
> x0 -> y0
> y0 -> {x1,x2}
> x1 -> y1
> y1 -> {x0,x2}
> x2 -> y2
> y2 -> {x0,x1}


Is this homework? Even if it isn't, would you perhaps show us what
you already have?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
er
Guest
Posts: n/a
 
      03-07-2008
On Mar 6, 6:19 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
> er wrote:
> > Hi all,

>
> > 1)

>
> > I have N=2 distinct pairs of objects {(x0,y0),(x1,y1)} related as

>
> > x0 -> y0
> > y0 -> {x1}
> > x1 -> y1
> > y1 -> {x0}

>
> > where -> represents, for example, has-a-ptr-to. Classes of objects
> > labeled x# and y# may have to support some operations (to safisfy
> > their left or right neighbors) but are of arbitrary type. So a class
> > for x# has to be like template<class Ty> class M_x{}, but also
> > template<class Tx> class M_y{},
> > which runs into a circular problem.

>
> > Any suggestion, please?

>
> > 2) How about N>2? for example N=3,

>
> > x0 -> y0
> > y0 -> {x1,x2}
> > x1 -> y1
> > y1 -> {x0,x2}
> > x2 -> y2
> > y2 -> {x0,x1}

>
> Is this homework? Even if it isn't, would you perhaps show us what
> you already have?
>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask


What I already have is a specific configuration {(M_x_fc,M_y_fc),
(M_x_fc,M_y_fc)} (see below). What i'd like to able to do is have an
arbitrary configuration, so long as the required operations are
supported. For example, M_x_fc requires that r_type support mean(),
and M_y_fc requires that l_type support x(); Please ask if still not
clear.


class M_x_fc{
public:
void update(double);
double x()const;
typedef M_y_fc r_type;
typedef boost::shared_ptr<const r_type> ptr_const_r_type;

private:
ptr_const_r_type ptr_const_r;
double _x;
};
void M_x_fc::update(double x_){
_x = x_;
};
double M_x_fc::mean()const{return ptr_const_r->mean();};


class M_y_fc{
public:
typedef M_x_fc l_type;
typedef boost::shared_ptr<const l_type> ptr_const_l_type;
void set(ptr_const_l_type ptr_l_compl);
double mean()const;
private:

double _rho;
double _mean;
ptr_const_l_type ptr_l_compl;
};
void M_y_fc::update(){
_mean = _rho*(ptr_l_compl->x());
};

double M_y_fc::mean()const{return _mean;};


class Config_fcfc{
public:
typedef M_x_fc l_fc_type;
typedef M_y_fc r_fc_type;
typedef boost::shared_ptr<l_fc_type> ptr_l_fc_type;
typedef boost::shared_ptr<r_fc_type> ptr_r_fc_type;
Config_fcfc(double rho);

ptr_r_fc_type ptr_r_fc_0;
ptr_r_fc_type ptr_r_fc_1;
ptr_l_fc_type ptr_l_fc_0;
ptr_l_fc_type ptr_l_fc_1;
};

Config_fcfc::Config_fcfc(double rho):
ptr_r_fc_0( new r_fc_type(0,rho)),
ptr_r_fc_1( new r_fc_type(1,rho)),
ptr_l_fc_0( new l_fc_type(ptr_r_fc_0)),
ptr_l_fc_1( new l_fc_type(ptr_r_fc_1))
{
ptr_r_fc_0->set(ptr_l_fc_1);
ptr_r_fc_1->set(ptr_l_fc_0);
};
 
Reply With Quote
 
er
Guest
Posts: n/a
 
      03-07-2008
On Mar 6, 7:09*pm, er <erwann.rog...@gmail.com> wrote:
> On Mar 6, 6:19 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
>
>
>
>
>
> > er wrote:
> > > Hi all,

>
> > > 1)

>
> > > I have N=2 distinct pairs of objects {(x0,y0),(x1,y1)} related as

>
> > > x0 -> y0
> > > y0 -> {x1}
> > > x1 -> y1
> > > y1 -> {x0}

>
> > > where -> represents, for example, has-a-ptr-to. Classes of objects
> > > labeled x# and y# may have to support some operations (to safisfy
> > > their left or right neighbors) but are of arbitrary type. So a class
> > > for x# has to be like template<class Ty> class M_x{}, but also
> > > template<class Tx> class M_y{},
> > > which runs into a circular problem.

>
> > > Any suggestion, please?

>
> > > 2) How about N>2? for example N=3,

>
> > > x0 -> y0
> > > y0 -> {x1,x2}
> > > x1 -> y1
> > > y1 -> {x0,x2}
> > > x2 -> y2
> > > y2 -> {x0,x1}

>
> > Is this homework? *Even if it isn't, would you perhaps show us what
> > you already have?

>
> > V
> > --
> > Please remove capital 'A's when replying by e-mail
> > I do not respond to top-posted replies, please don't ask

>
> What I already have is a specific configuration {(M_x_fc,M_y_fc),
> (M_x_fc,M_y_fc)} (see below). What i'd like to able to do is have an
> arbitrary configuration, so long as the required operations are
> supported. For example, M_x_fc requires that r_type support mean(),
> and M_y_fc requires that l_type support x(); Please ask if still not
> clear.
>
> * * * * * * * * class M_x_fc{
> * * * * * * * * * * * * public:
> * * * * * * * * * * * * * * * * void update(double);
> * * * * * * * * * * * * * * * * double x()const;
> * * * * * * * * * * * * * * * * typedef M_y_fc * * * * * * * * * * * * * * * * * * * * *r_type;
> * * * * * * * * * * * * * * * * typedef boost::shared_ptr<const r_type> * * * * * * * * * ptr_const_r_type;
>
> * * * * * * * * * * * * private:
> * * * * * * * * * * * * * * * * ptr_const_r_type ptr_const_r;
> * * * * * * * * * * * * * * * * double _x;
> * * * * * * * * };
> * * * * * * * * void M_x_fc::update(double x_){
> * * * * * * * * * * * * _x = x_;
> * * * * * * * * };
> * * * * * * * * double M_x_fc::mean()const{return ptr_const_r->mean();};
>
> * * * * * * * * class M_y_fc{
> * * * * * * * * * * * * public:
> * * * * * * * * * * * * * * * * typedef M_x_fc * * * * * * * * * * * * * * * * *l_type;
> * * * * * * * * * * * * * * * * typedef boost::shared_ptr<const l_type> * * * * * ptr_const_l_type;
> * * * * * * * * * * * * * * * * void set(ptr_const_l_type ptr_l_compl);
> * * * * * * * * * * * * * * * * double mean()const;
> * * * * * * * * * * * * private:
>
> * * * * * * * * * * * * * * * * double * * * * *_rho;
> * * * * * * * * * * * * * * * * double * * * * *_mean;
> * * * * * * * * * * * * * * * * ptr_const_l_type ptr_l_compl;
> * * * * * * * * };
> * * * * * * * * void M_y_fc::update(){
> * * * * * * * * * * * * _mean = _rho*(ptr_l_compl->x());
> * * * * * * * * };
>
> * * * * * * * * double M_y_fc::mean()const{return _mean;};
>
> * * * * * * * * class Config_fcfc{
> * * * * * * * * * * * * public:
> * * * * * * * * * * * * * * * * typedef M_x_fc * * * * * * * * * * * * * * * * * * * * * * * * * * * * *l_fc_type;
> * * * * * * * * * * * * * * * * typedef M_y_fc * * * * * * * * * * * * * * * * * * * * * * * * * * * * *r_fc_type;
> * * * * * * * * * * * * * * * * typedef boost::shared_ptr<l_fc_type> * * * * * * * * * * * * * * * * * * *ptr_l_fc_type;
> * * * * * * * * * * * * * * * * typedef boost::shared_ptr<r_fc_type> * * * * * * * * * * * * * * * * * * *ptr_r_fc_type;
> * * * * * * * * * * * * * * * * Config_fcfc(double rho);
>
> * * * * * * * * * * * * * * * * ptr_r_fc_type * ptr_r_fc_0;
> * * * * * * * * * * * * * * * * ptr_r_fc_type * ptr_r_fc_1;
> * * * * * * * * * * * * * * * * ptr_l_fc_type * ptr_l_fc_0;
> * * * * * * * * * * * * * * * * ptr_l_fc_type * ptr_l_fc_1;
> * * * * * * * * };
>
> * * * * * * * * Config_fcfc::Config_fcfc(double rho):
> * * * * * * * * * * * * ptr_r_fc_0( new r_fc_type(0,rho)),
> * * * * * * * * * * * * ptr_r_fc_1( new r_fc_type(1,rho)),
> * * * * * * * * * * * * ptr_l_fc_0( new l_fc_type(ptr_r_fc_0)),
> * * * * * * * * * * * * ptr_l_fc_1( new l_fc_type(ptr_r_fc_1))
> * * * * * * * * {
> * * * * * * * * * * * * ptr_r_fc_0->set(ptr_l_fc_1);
> * * * * * * * * * * * * ptr_r_fc_1->set(ptr_l_fc_0);
> * * * * * * * * };- Hide quoted text -
>
> - Show quoted text -


ps: i'm aware that this problem can be solved by breaking dependencies
using inheritance but i'm interested in a fully generic solution.
knowing if that generic solution does not exist would already help.
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      03-07-2008
er wrote:
> On Mar 6, 6:19 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
>> er wrote:
>>> Hi all,

>>
>>> 1)

>>
>>> I have N=2 distinct pairs of objects {(x0,y0),(x1,y1)} related as

>>
>>> x0 -> y0
>>> y0 -> {x1}
>>> x1 -> y1
>>> y1 -> {x0}

>>
>>> where -> represents, for example, has-a-ptr-to. Classes of objects
>>> labeled x# and y# may have to support some operations (to safisfy
>>> their left or right neighbors) but are of arbitrary type. So a class
>>> for x# has to be like template<class Ty> class M_x{}, but also
>>> template<class Tx> class M_y{},
>>> which runs into a circular problem.

>>
>>> Any suggestion, please?

>>
>>> 2) How about N>2? for example N=3,

>>
>>> x0 -> y0
>>> y0 -> {x1,x2}
>>> x1 -> y1
>>> y1 -> {x0,x2}
>>> x2 -> y2
>>> y2 -> {x0,x1}

>>
>> Is this homework? Even if it isn't, would you perhaps show us what
>> you already have?
>>
>> V
>> --
>> Please remove capital 'A's when replying by e-mail
>> I do not respond to top-posted replies, please don't ask

>
> What I already have is a specific configuration {(M_x_fc,M_y_fc),
> (M_x_fc,M_y_fc)} (see below). What i'd like to able to do is have an
> arbitrary configuration, so long as the required operations are
> supported. For example, M_x_fc requires that r_type support mean(),
> and M_y_fc requires that l_type support x(); Please ask if still not
> clear.
>
>
> class M_x_fc{
> public:
> void update(double);
> double x()const;
> typedef M_y_fc r_type;
> typedef boost::shared_ptr<const r_type> ptr_const_r_type;
>
> private:
> ptr_const_r_type ptr_const_r;
> double _x;
> };
> void M_x_fc::update(double x_){
> _x = x_;
> };
> double M_x_fc::mean()const{return ptr_const_r->mean();};
>
>
> class M_y_fc{
> public:
> typedef M_x_fc l_type;
> typedef boost::shared_ptr<const l_type> ptr_const_l_type;
> void set(ptr_const_l_type ptr_l_compl);
> double mean()const;
> private:
>
> double _rho;
> double _mean;
> ptr_const_l_type ptr_l_compl;
> };
> void M_y_fc::update(){
> _mean = _rho*(ptr_l_compl->x());
> };
>
> double M_y_fc::mean()const{return _mean;};
>
>
> class Config_fcfc{
> public:
> typedef M_x_fc l_fc_type;
> typedef M_y_fc r_fc_type;
> typedef boost::shared_ptr<l_fc_type> ptr_l_fc_type;
> typedef boost::shared_ptr<r_fc_type> ptr_r_fc_type;
> Config_fcfc(double rho);
>
> ptr_r_fc_type ptr_r_fc_0;
> ptr_r_fc_type ptr_r_fc_1;
> ptr_l_fc_type ptr_l_fc_0;
> ptr_l_fc_type ptr_l_fc_1;
> };
>
> Config_fcfc::Config_fcfc(double rho):
> ptr_r_fc_0( new r_fc_type(0,rho)),
> ptr_r_fc_1( new r_fc_type(1,rho)),
> ptr_l_fc_0( new l_fc_type(ptr_r_fc_0)),
> ptr_l_fc_1( new l_fc_type(ptr_r_fc_1))
> {
> ptr_r_fc_0->set(ptr_l_fc_1);
> ptr_r_fc_1->set(ptr_l_fc_0);
> };


It's still unclear what exactly you need to do and can't.

template<class T> class M_x;
template<class T> class M_y;

template<class T> class M_x {
typedef M_y<T> r_type;
...
};

template<class T> class M_x {
typedef M_x<T> l_type;
...
};

Make sure you define all members _after_ both class templates
are defined, and not after _each_ class template.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Circular Templates cv_curious C++ 3 12-14-2010 08:56 AM
how to Specializations of function Templates or Overloading Function templates with Templates ? recover C++ 2 07-25-2006 02:55 AM
Circular dependencies in Templates - better solution? ro86 C++ 4 03-06-2005 10:10 PM
Semi-circular definitions (plus circular references) Kiuhnm C++ 16 01-03-2005 03:49 AM
Templates templates templates JKop C++ 3 07-21-2004 11:44 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57