Need some help with advanced templates

Discussion in 'C++' started by rami, Jan 17, 2005.

  1. rami

    rami Guest

    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
    rami, Jan 17, 2005
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Jan 17, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest
    Replies:
    0
    Views:
    660
    Guest
    Feb 25, 2004
  2. JKop
    Replies:
    3
    Views:
    439
  3. Domino
    Replies:
    5
    Views:
    366
    dorayme
    Nov 5, 2006
  4. recover
    Replies:
    2
    Views:
    778
    recover
    Jul 25, 2006
  5. Michele Simionato
    Replies:
    1
    Views:
    579
    Lacrima
    Mar 27, 2010
Loading...

Share This Page