Need some help with advanced templates



 DEVELOP > c-Plus-Plus > Need some help with advanced templates

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "rami"
Date: 17 Jan 2005 10:30:15 AM
Object: Need some help with advanced templates
I have some code which does following thing
template<class X, unsigned ID = 0>
struct SomeStruct
{
template<class X>
static SomeStruct<X, ID + 1>& SomeFunc();
...
...
...
...
};
It has more than one overloads of SomeFunc and some other function
which have same return types.
Well i can understand the code pretty much but what i need to
understand is why ID is being used and increamented everytime on the
return?
I think its to avoid the compiler to use the same instiation - but i go
blank when i try to expand on my thought :-|
Any help would be appreciated, in case the example is vague i can maybe
post more..
Regards,
Rami
.

User: "Victor Bazarov"

Title: Re: Need some help with advanced templates 17 Jan 2005 10:48:33 AM
rami wrote:

I have some code which does following thing
template<class X, unsigned ID = 0>
struct SomeStruct
{
template<class X>
static SomeStruct<X, ID + 1>& SomeFunc();
..
..
..
..
};

It has more than one overloads of SomeFunc and some other function
which have same return types.

Well i can understand the code pretty much but what i need to
understand is why ID is being used and increamented everytime on the
return?

How can anyone tell without seeing how the ID argument is used?

I think its to avoid the compiler to use the same instiation - but i go
blank when i try to expand on my thought :-|

It is usually to use the other instantiation (not to avoid using the same
instantiation). IOW, it's to _chain_ the classes:
SomeStruct<int,10> blah;
blah.SomeFunc<int>().SomeFunc<int>().SomeFunc<int>();
will cause the instantiation of SomeStruct<int,10> and [probably] also
SomeStruct<int,11>, SomeStruct<int,12>, and SomeStruct<int,13> since they
are the return value types of the three calls.

Any help would be appreciated, in case the example is vague i can maybe
post more..

You don't have to post more for now. Try to understand the role of 'ID'
and how advancing it achieves some goal and what that goal might be. If
you do fail to understand it, post again, with more concrete code and
questions.
Good luck!
V
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER