473,729 Members | 2,151 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 beyondAllReachi ng;

// 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.beyondAllReac hing = r.beyondAllReac hing;
}

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 1690
Andy Champ wrote:
We have a class with something that, simplified, looks like this:

template <typename Tclass foo
{
T beyondAllReachi ng;

// 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.beyondAllReac hing = r.beyondAllReac hing;
}

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<typena me Tclass foo
{
T beyondAllReachi ng;
template<class S, class Ufriend void method(foo<Sl, foo<Ur);
};

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

foo<doublefd;
fd.beyondAllRea ching = 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
3342
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 to turn a previously made String class that deals with char's into a templated String class that uses the template parameter C instead of char. I thought it would be fairly simple to do this exercise, but I encoutered many errors for my...
5
8958
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 to work properly; it generates a link error. Thank you, Oplec. #include <cstdlib>
2
2437
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 SLinkList.cxx:3, from list.cxx:1: SLinkList.h:20: ISO C++ forbids declaration of `type name' with no
11
2599
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 by myself. I know there are already tons of vector and matrix implementations, but I wanted to have one taylored for my needs and without debugging someones else code. Also is's become somewhat personal meanwhile ;-).
1
1862
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
3860
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
1603
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 <class B,class Cclass A; //forward declaration template <class DBT_> class D{ public: friend class A<B,C>; //Error as B and C are not known as template
7
4269
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> inline const T1 min (const T1& a, const T2& b) { return (a < b ? a : b);
6
1929
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
9289
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9210
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9156
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8159
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4534
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4798
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3242
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2694
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.