473,397 Members | 2,084 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 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 1824
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Christophe Barbe | last post by:
I am not clear about friend functions of a template class. GCC (3.3.2) wants me to add <> after the friend function names in the class declaration and VisualC++ doesn't like that. template...
1
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me...
5
by: Oplec | last post by:
Hi, I am trying to figure out the correct syntax for declaring a friend function in a class template and then defining that member after the class. The code example below is what I am trying to get...
1
by: Dmitry D | last post by:
Hi all, I'm having problems with declaring a template friend function. It seems like I've done everything as explained in C++ FAQ, but still, I'm getting the linker error (unresolved external...
2
by: Ruben Campos | last post by:
I have a problem with a template function that is declared as a friend of a template class. I'll first show the exact problem with source code: // MyClass.hpp template <typename T> class...
5
by: Levent | last post by:
Hi, Why doesn't this work? (tried with gcc 3.3.3 and VC++ 7.1): #include <iostream> template<class T, unsigned N> struct Foo { void func(); }; template<class T, unsigned N>
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
3
by: jdurancomas | last post by:
Dear all, I'm trying to declare the operator++ to a nested class. The nested class is not template but the container it is. The code used in teh sample program is included bellow: ...
2
by: Andy Champ | last post by:
We have a class with something that, simplified, looks like this: template <typename Tclass foo { T beyondAllReaching; // In VC2005, this works OK template <typename Ufriend void...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.