syntax: howto write partial template friend declaration

Discussion in 'C++' started by farseerfc, Sep 26, 2008.

  1. farseerfc

    farseerfc Guest

    Hi, everyone here loves C++;
    As a student studying data structure, I'm writting a single list (with only
    one pointer to identity the next node in its node struct) simulating
    std::list like:

    template <typename T, typename Alloc> class slist {...};

    Here Alloc accepts a std::allcator . And within the same namespace I wrote
    its iterator:

    template <typename T> class slist_iterator {...};

    the problem is that when I want to say slist is slist_iterator's friend, I
    can't find a syntax to express exactly what I want, I mean when I write in
    side slist_iterator:

    //inside slist_iterator
    template <typename Ty,typename Alloc> friend class slist;

    all kinds of slists is the friend of slist_iterator now, ie,
    slist<double,std::allocator<double> > is also a friend of
    slist_iterator<int> , that is not what I want.And I can not write this:

    friend class slist<T,Alloc>;

    because inside slist_iterator there is no information about Alloc.
    What I want is a partial template friend declaration, something like a
    partial template specialization:

    template<typename Alloc> friend class slist<T,Alloc>;

    that means all slist with the same element type is the friend of
    slist_iterator, but no more further.
    But in all compilers I tested, the sentence above is a syntax error.
    So can I / howto write this , is there any syntax support or
    metaprogramming tech to achieve this ?
    farseerfc, Sep 26, 2008
    #1
    1. Advertising

  2. farseerfc

    Triple-DES Guest

    On 26 Sep, 08:21, "farseerfc" <> wrote:
    > Hi, everyone here loves C++;
    > As a student studying data structure, I'm writting a single list (with only
    > one pointer to identity the next node in its node struct) simulating
    > std::list like:
    >
    > template <typename T, typename Alloc> class slist {...};
    >
    > Here Alloc accepts a std::allcator . And within the same namespace I wrote
    > its iterator:
    >
    > template <typename T> class slist_iterator {...};
    >
    > the problem is that when I want to say slist is slist_iterator's friend, I
    > can't find a syntax to express exactly what I want, I mean when I write in
    > side slist_iterator:
    >
    > //inside slist_iterator
    > template <typename Ty,typename Alloc> friend class slist;
    >
    > all kinds of slists is the friend of slist_iterator now, ie,
    > slist<double,std::allocator<double> > is also a friend of
    > slist_iterator<int> , that is not what I want.And I can not write this:
    >
    > friend class slist<T,Alloc>;
    >
    > because inside slist_iterator there is no information about Alloc.
    > What I want is a partial template friend declaration, something like a
    > partial template specialization:
    >
    > template<typename Alloc> friend class slist<T,Alloc>;
    >
    > that means all slist with the same element type is the friend of
    > slist_iterator, but no more further.
    > But in all compilers I tested, the sentence above is a syntax error.
    > So  can I / howto write this , is there any syntax support or
    > metaprogramming tech to achieve this ?


    First of all, why do you need class slist to be a friend of
    slist_iterator?
    Are you sure you don't want it the other way around? (let the iterator
    access the list's private members)

    Because that would be pretty simple:
    template<typename T, typename Alloc> class slist
    {
    friend class slist_iterator<T>;
    // ...
    };

    DP
    Triple-DES, Sep 26, 2008
    #2
    1. Advertising

  3. farseerfc

    farseerfc Guest

    First of all, thank you;
    I just simply want the list have access to the private member of the
    iterator, many member functions of list(like merge,reverse,etc.) needs this
    to change the order of the nodes with no creating/deleting nodes.

    Maybe I can write iterator inside list to achieve this (maybe, I haven't
    test yet.) And to be frank, this is a part of assignments of my data
    structure course, and according to the requirement of the assignment, these
    two classes should be apart from each other.

    The most interesting thing is the thinking why C++ doesn't supply such kind
    of syntax like "partial template friend declaration" . We have already got
    template friend ... I try to image that there is some kind of
    metaprogramming skill can achieve this goal so I asked it here

    ;-P

    tkank you,and thank you all


    "Triple-DES" <> wrote
    news:...
    > On 26 Sep, 08:21, "farseerfc" <> wrote:
    >> Hi, everyone here loves C++;
    >> As a student studying data structure, I'm writting a single list (with
    >> only
    >> one pointer to identity the next node in its node struct) simulating
    >> std::list like:
    >>
    >> template <typename T, typename Alloc> class slist {...};
    >>
    >> Here Alloc accepts a std::allcator . And within the same namespace I
    >> wrote
    >> its iterator:
    >>
    >> template <typename T> class slist_iterator {...};
    >>
    >> the problem is that when I want to say slist is slist_iterator's friend,
    >> I
    >> can't find a syntax to express exactly what I want, I mean when I write
    >> in
    >> side slist_iterator:
    >>
    >> //inside slist_iterator
    >> template <typename Ty,typename Alloc> friend class slist;
    >>
    >> all kinds of slists is the friend of slist_iterator now, ie,
    >> slist<double,std::allocator<double> > is also a friend of
    >> slist_iterator<int> , that is not what I want.And I can not write this:
    >>
    >> friend class slist<T,Alloc>;
    >>
    >> because inside slist_iterator there is no information about Alloc.
    >> What I want is a partial template friend declaration, something like a
    >> partial template specialization:
    >>
    >> template<typename Alloc> friend class slist<T,Alloc>;
    >>
    >> that means all slist with the same element type is the friend of
    >> slist_iterator, but no more further.
    >> But in all compilers I tested, the sentence above is a syntax error.
    >> So can I / howto write this , is there any syntax support or
    >> metaprogramming tech to achieve this ?

    >
    > First of all, why do you need class slist to be a friend of
    > slist_iterator?
    > Are you sure you don't want it the other way around? (let the iterator
    > access the list's private members)
    >
    > Because that would be pretty simple:
    > template<typename T, typename Alloc> class slist
    > {
    > friend class slist_iterator<T>;
    > // ...
    > };
    >
    > DP
    >
    farseerfc, Sep 26, 2008
    #3
  4. farseerfc

    Triple-DES Guest

    On 26 Sep, 09:48, "farseerfc" <> wrote:
    > First of all, thank you;
    > I just simply want the list have access to the private member of the
    > iterator, many member functions of list(like merge,reverse,etc.) needs this
    > to change the order of the nodes with no creating/deleting nodes.
    >
    > Maybe I can write iterator inside list to achieve this (maybe, I haven't
    > test yet.) And to be frank, this is a part of assignments of my data
    > structure course, and according to the requirement of the assignment, these
    > two classes should be apart from each other.
    >
    > The most interesting thing is the thinking why C++ doesn't supply such kind
    > of syntax like "partial template friend declaration" . We have already got
    > template friend ... I try to image that there is some kind of
    > metaprogramming skill can achieve this goal so I asked it here
    >
    > ;-P
    >
    > tkank you,and thank you all


    In that case I would simply add the Alloc as a template argument to
    the iterator. Remember that iterators of slists with different
    allocators should be different types. With your current design, they
    are not.

    template<typename T, typename Alloc>
    class slist_iterator
    {
    friend class slist<T, Alloc>
    // ...
    };
    Triple-DES, Sep 26, 2008
    #4
  5. farseerfc

    farseerfc Guest

    In this case, iterators of slists with different allocators are not the
    same, but I still think the syntax should be useful in other cases.

    Actually,in my design, there is another template parameter bool circle to
    indicate whether this slist should be a circle single list( whether the
    last_node->next should be NULL or slist's head):

    template <typename T,bool circle, typename Alloc> class slist;

    And the iterator of these two kinds of slist should be the same class
    because merge a sequence of non-circle slist into a circle slist is
    reasonary, and vise versa as long as the sequence doesn't contain the head
    node( which is used as the mark of the end of the circle slist and does not
    store value).That means I wants to write something like this in the
    iterator:

    template<bool b> friend class slist<T,b,Alloc>;

    Still in this case I can add to friend declare to avoid "partial".
    But I do believe this partial syntax should be useful in some certain cases.


    "Triple-DES" <> wrote
    news:...
    > On 26 Sep, 09:48, "farseerfc" <> wrote:
    >> First of all, thank you;
    >> I just simply want the list have access to the private member of the
    >> iterator, many member functions of list(like merge,reverse,etc.) needs
    >> this
    >> to change the order of the nodes with no creating/deleting nodes.
    >>
    >> Maybe I can write iterator inside list to achieve this (maybe, I haven't
    >> test yet.) And to be frank, this is a part of assignments of my data
    >> structure course, and according to the requirement of the assignment,
    >> these
    >> two classes should be apart from each other.
    >>
    >> The most interesting thing is the thinking why C++ doesn't supply such
    >> kind
    >> of syntax like "partial template friend declaration" . We have already
    >> got
    >> template friend ... I try to image that there is some kind of
    >> metaprogramming skill can achieve this goal so I asked it here
    >>
    >> ;-P
    >>
    >> tkank you,and thank you all

    >
    > In that case I would simply add the Alloc as a template argument to
    > the iterator. Remember that iterators of slists with different
    > allocators should be different types. With your current design, they
    > are not.
    >
    > template<typename T, typename Alloc>
    > class slist_iterator
    > {
    > friend class slist<T, Alloc>
    > // ...
    > };
    farseerfc, Sep 26, 2008
    #5
  6. farseerfc

    farseerfc Guest

    Sorry,I mean I can add two friend declarations to avoid "partial" But...

    "farseerfc" <> wrote news:gbi6lg$4i0$99.com...
    > In this case, iterators of slists with different allocators are not the
    > same, but I still think the syntax should be useful in other cases.
    >
    > Actually,in my design, there is another template parameter bool circle to
    > indicate whether this slist should be a circle single list( whether the
    > last_node->next should be NULL or slist's head):
    >
    > template <typename T,bool circle, typename Alloc> class slist;
    >
    > And the iterator of these two kinds of slist should be the same class
    > because merge a sequence of non-circle slist into a circle slist is
    > reasonary, and vise versa as long as the sequence doesn't contain the head
    > node( which is used as the mark of the end of the circle slist and does
    > not store value).That means I wants to write something like this in the
    > iterator:
    >
    > template<bool b> friend class slist<T,b,Alloc>;
    >
    > Still in this case I can add to friend declare to avoid "partial".
    > But I do believe this partial syntax should be useful in some certain
    > cases.
    >
    >
    > "Triple-DES" <> wrote
    > news:...
    >> On 26 Sep, 09:48, "farseerfc" <> wrote:
    >>> First of all, thank you;
    >>> I just simply want the list have access to the private member of the
    >>> iterator, many member functions of list(like merge,reverse,etc.) needs
    >>> this
    >>> to change the order of the nodes with no creating/deleting nodes.
    >>>
    >>> Maybe I can write iterator inside list to achieve this (maybe, I haven't
    >>> test yet.) And to be frank, this is a part of assignments of my data
    >>> structure course, and according to the requirement of the assignment,
    >>> these
    >>> two classes should be apart from each other.
    >>>
    >>> The most interesting thing is the thinking why C++ doesn't supply such
    >>> kind
    >>> of syntax like "partial template friend declaration" . We have already
    >>> got
    >>> template friend ... I try to image that there is some kind of
    >>> metaprogramming skill can achieve this goal so I asked it here
    >>>
    >>> ;-P
    >>>
    >>> tkank you,and thank you all

    >>
    >> In that case I would simply add the Alloc as a template argument to
    >> the iterator. Remember that iterators of slists with different
    >> allocators should be different types. With your current design, they
    >> are not.
    >>
    >> template<typename T, typename Alloc>
    >> class slist_iterator
    >> {
    >> friend class slist<T, Alloc>
    >> // ...
    >> };

    >
    >
    farseerfc, Sep 26, 2008
    #6
  7. farseerfc

    Triple-DES Guest

    On 26 Sep, 10:34, "farseerfc" <> wrote:
    > Sorry,I mean I can add two friend declarations to avoid "partial" But...
    > "farseerfc" <> wrotenews:gbi6lg$4i0$99.com....
    > > In this case, iterators of slists with different allocators are not the
    > > same, but I still think the syntax should be useful in other cases.

    >
    > > Actually,in my design, there is another template parameter bool circle to
    > > indicate whether this slist should be a circle single list( whether the
    > > last_node->next should be NULL or slist's head):

    >
    > > template <typename T,bool circle, typename Alloc> class slist;

    >
    > > And the iterator of these two kinds of slist should be the same class
    > > because merge a sequence of non-circle slist into a circle slist is
    > > reasonary, and vise versa as long as the sequence doesn't contain the head
    > > node( which is used as the mark of the end of the circle slist and does
    > > not store value).That means I wants to write something like this in the
    > > iterator:

    >
    > > template<bool b> friend class slist<T,b,Alloc>;

    >
    > > Still in this case I can add to friend declare to avoid "partial".
    > > But I do believe this partial syntax should be useful in some certain
    > > cases.

    >
    > > "Triple-DES" <> wrote
    > >news:....
    > >> On 26 Sep, 09:48, "farseerfc" <> wrote:
    > >>> First of all, thank you;
    > >>> I just simply want the list have access to the private member of the
    > >>> iterator, many member functions of list(like merge,reverse,etc.) needs
    > >>> this
    > >>> to change the order of the nodes with no creating/deleting nodes.

    >
    > >>> Maybe I can write iterator inside list to achieve this (maybe, I haven't
    > >>> test yet.) And to be frank, this is a part of assignments of my data
    > >>> structure course, and according to the requirement of the assignment,
    > >>> these
    > >>> two classes should be apart from each other.

    >
    > >>> The most interesting thing is the thinking why C++ doesn't supply such
    > >>> kind
    > >>> of syntax like "partial template friend declaration" . We have already
    > >>> got
    > >>> template friend ... I try to image that there is some kind of
    > >>> metaprogramming skill can achieve this goal so I asked it here

    >
    > >>> ;-P

    >
    > >>> tkank you,and thank you all

    >
    > >> In that case I would simply add the Alloc as a template argument to
    > >> the iterator. Remember that iterators of slists with different
    > >> allocators should be different types. With your current design, they
    > >> are not.

    >
    > >> template<typename T, typename Alloc>
    > >> class slist_iterator
    > >> {
    > >>  friend class slist<T, Alloc>
    > >>  // ...
    > >> };


    Yes, I agree it would be useful in some cases. Unfortunately, it is
    not allowed by the current standard, and as far as I know it won't
    change in C++0x.
    Triple-DES, Sep 26, 2008
    #7
    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. john smith
    Replies:
    1
    Views:
    1,910
    Victor Bazarov
    Aug 10, 2003
  2. Yu Lianqing
    Replies:
    1
    Views:
    639
    Leor Zolman
    Apr 4, 2004
  3. toton
    Replies:
    1
    Views:
    612
  4. A L
    Replies:
    1
    Views:
    510
    Alf P. Steinbach /Usenet
    Aug 25, 2010
  5. Andrey Dj
    Replies:
    3
    Views:
    740
    Andrey Dj
    Apr 5, 2012
Loading...

Share This Page