Need some help with advanced templates

R

rami

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
 
V

Victor Bazarov

rami said:
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 said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top