473,779 Members | 2,089 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 1694
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
3343
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
8960
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
2442
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
2604
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
1864
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
3861
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
1604
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
4270
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
1934
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
9636
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10306
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10139
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
10075
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
8961
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...
0
5373
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...
1
4037
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
3632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2869
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.