Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Need some help with advanced templates

Reply
Thread Tools

Need some help with advanced templates

 
 
rami
Guest
Posts: n/a
 
      01-17-2005
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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-17-2005
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
 
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
Re: Advanced Python Programming Oxford Lectures [was: Re: *Advanced*Python book?] Michele Simionato Python 1 03-27-2010 06:10 AM
how to Specializations of function Templates or Overloading Function templates with Templates ? recover C++ 2 07-25-2006 02:55 AM
Monster Templates - Question about Submitting Templates Fred HTML 1 09-26-2005 01:09 AM
Templates templates templates JKop C++ 3 07-21-2004 11:44 AM
using templates in templates John Harrison C++ 8 07-31-2003 12:00 PM



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