473,396 Members | 2,030 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,396 software developers and data experts.

What's the correct syntax for a friend with template parameters?

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 method(foo<Tl, foo<Ur);

// But VC2008 requires this
template <typename U, class Vfriend void method(U l, V r);
};

// To allow the actual function to access the private data.
template <typename T, typename Uvoid method(foo<Tl, foo<Ur)
{
l.beyondAllReaching = r.beyondAllReaching;
}

It occurs to me writing this that method<int, charis not a friend of
foo<char- so I'm a bit surprised that either works. But that isn't
the main point. I want to know:

- Which is the correct syntax?
- Is there a way to stop a method<string, stringaccessing the privates
of a foo<char>?

Thanks

Andy
Jul 9 '08 #1
2 1659
Andy Champ wrote:
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 method(foo<Tl, foo<Ur);

// But VC2008 requires this
template <typename U, class Vfriend void method(U l, V r);
};

// To allow the actual function to access the private data.
template <typename T, typename Uvoid method(foo<Tl, foo<Ur)
This is a function template with 2 template arguments. Shouldn't the
'friend' declaration also have 2 template arguments? That's the reason
for the correct syntax (you marked it as VC2008).
{
l.beyondAllReaching = r.beyondAllReaching;
}

It occurs to me writing this that method<int, charis not a friend of
foo<char- so I'm a bit surprised that either works.
Actually, that's not true, the friend declaration declares *any*
instantiation of 'method' function template to be a friend of *any*
'foo' class template instantiation.
But that isn't
the main point. I want to know:

- Which is the correct syntax?
To declare what, exactly?
- Is there a way to stop a method<string, stringaccessing the privates
of a foo<char>?
'fraid not. Partial specialisations aren't allowed in friend
declarations. Also, there are no partial specialisations of function
templates.

template<class Tclass foo;
template<class A, class Bvoid method(foo<A>, foo<B>);

template<typename Tclass foo
{
T beyondAllReaching;
template<class S, class Ufriend void method(foo<Sl, foo<Ur);
};

template<class A, class Bvoid method(foo<Afa, foo<Bfb)
{
fa.beyondAllReaching = 0;
fb.beyondAllReaching = 0;

foo<doublefd;
fd.beyondAllReaching = 3.14159; // oops. Didn't want that...
}

int main()
{
foo<intfi;
foo<charfc;

method(fi, fc);
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 9 '08 #2
Victor Bazarov wrote:
>
Actually, that's not true, the friend declaration declares *any*
instantiation of 'method' function template to be a friend of *any*
'foo' class template instantiation.
Ah. That's not *quite* what I wanted, but that's the language so...

Thanks

Andy
Jul 10 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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...
2
by: Yu Lianqing | last post by:
Hi, all I am writing an overloading operator >> function for a template class and can't make it right. G++ 3.2 (Redhat8.0) gives the following errors: g++ -c list.cxx In file included from...
11
by: Micha | last post by:
Hello there, I think I've run into some classic c++ pitfall and maybe some of you guys can help me out. For my project I will need to use matrices and vectors and so I decided to implement them...
1
by: Vincent RICHOMME | last post by:
Hi, I have class A and I would like to declare the following templated class as a friend template <typename Tenum, typename Tproperties> class CEnumBinder
8
by: Paul Roberts | last post by:
Hi, I'm hoping somebody here can help me with a simple problem of template syntax. Here's an example: template<typename T, int iclass A { static int a;
7
by: Nike | last post by:
The following code doesn't compile. I have a requirement in my project to declare one template as a friend function to another.. Can anyone suggest a way???? #include <iostream.h> template...
7
by: mosfet | last post by:
HI, when trying to compile an embedded version of STL called ustl on win32 platform I get the following error : /// Returns the minimum of \p a and \p b template <typename T1, typename T2>...
6
by: puzzlecracker | last post by:
Say we have this structure: Struct Foo{ .... friend ostream& operator << (ostream& s, Foo & m); ..... }; friend ostream& operator << (ostream& s, Foo & m){
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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.