471,616 Members | 1,397 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,616 software developers and data experts.

syntax: howto write partial template friend declaration

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 Allocclass slist {...};

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

template <typename Tclass 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 Allocfriend 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 Allocfriend 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 ?

Sep 26 '08 #1
1 1690
On 26 Sep, 08:21, "farseerfc" <farsee...@sjtu.edu.cnwrote:
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 Allocclass slist {...};

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

template <typename Tclass 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 Allocfriend 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 Allocfriend 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 Allocclass slist
{
friend class slist_iterator<T>;
// ...
};

DP
Sep 26 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Christophe Barbe | last post: by
1 post views Thread by Dmitry D | last post: by
2 posts views Thread by Ruben Campos | last post: by
3 posts views Thread by Hamilton Woods | last post: by
3 posts views Thread by jdurancomas | last post: by
1 post views Thread by ZEDKYRIE | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.