473,473 Members | 1,614 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

template friend classes

17 New Member
I'd like to avoid listing all the functions in a template class as friends by declaring the whole class as a friend, but...

Expand|Select|Wrap|Line Numbers
  1. template<class T1> class A;
  2. template<class T1, class T2> class B;
  3.  
  4. template<class T1> class C {
  5.  
  6.       friend void A<T1>::f1(void);
  7.       friend void A<T1>::f2(void);
  8.       friend void A<T1>::f3(void);
  9.  
  10.       friend class A<T1>;
  11.  
  12.       template<class T2> friend void B<T1,T2>::f1(void);
  13.       template<class T2> friend void B<T1,T2>::f2(void);
  14.       template<class T2> friend void B<T1,T2>::f3(void);
  15.  
  16.       /*
  17.        * why is everything above allowed, 
  18.        * but not the following line?
  19.        */
  20.  
  21.       template<class T2> friend class B<T1,T2>; 
  22. // error: partial specialization 'B<T1, T2>' declared 'friend'
  23.  
  24. };
  25.  
  26.  
Am I missing the obvious?

I'm using i686-apple-darwin10-g++-4.2.1
Jun 12 '13 #1
3 1472
weaknessforcats
9,208 Recognized Expert Moderator Expert
This code:
Expand|Select|Wrap|Line Numbers
  1. template<class T1> class C {
  2.  
  3.  <code removed ...>
  4.       friend class A<T1>;
  5.  
  6.       template<class T2> friend void B<T1,T2>::f1(void);
  7.       template<class T2> friend void B<T1,T2>::f2(void);
  8.       template<class T2> friend void B<T1,T2>::f3(void);
  9.  
  10.       /*
  11.        * why is everything above allowed, 
  12.        * but not the following line?
  13.        */
  14.  
  15.       template<class T2> friend class B<T1,T2>; 
  16. // error: partial specialization 'B<T1, T2>' declared 'friend'
  17.  
  18. };
  19.  
In the above class template for C, type T1 is declared but suddenly a T2 appears. That makes this code not compilable:

Expand|Select|Wrap|Line Numbers
  1. template<class T2> friend void B<T1,T2>::f3(void);
  2.  
Change your class to:
Expand|Select|Wrap|Line Numbers
  1. template<class T1, class T2> class C {
  2. etc...
  3.  
Then change the friend functions to:
Expand|Select|Wrap|Line Numbers
  1.  template<class T1, class T2> friend void B<T1,T2>::f1(void);
  2.  
Finally, change your friend class to:
Expand|Select|Wrap|Line Numbers
  1. friend class B<T1 ,T2>;
  2.  
There will still be a problem with your friend member functions. In your C class template, the member functions are parameterized with T1 and T2. But there is no template for this function so the compiler doesn't now how to specialize it. You will need, in addition to the above changes, provide a function template for you class B member functions. I used:

Expand|Select|Wrap|Line Numbers
  1. template<class T1, class T2> class B
  2. {
  3. public:
  4.       void f1(void);
  5. };
  6.  
  7. template<class T1, class T2> void B<T1,T2>::f1(void) {}
  8.  
When you use a function template inside another template, the inner template is either bound or unbound. It is bound when T1 and T2 are known types. In this case you can use a function prototype (as you did) since the template function already has been created. However, when T1 or T2 are not known, then you must supply the full template before the compiler sees the function prototype.
Jun 12 '13 #2
tanvalley
17 New Member
I need a single instance of C<T1> to be accessible by B<T1,T2>::f1() and by B<T1,T3>::f1(). By naming the functions individually (in my real case there are more of them) I can allow all of the members of each B<T1,x> access to the internals of C<T1>. I don't want to change my privates.
Jun 12 '13 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
Why not just create a C<T1> singleton object and access it from your other objects using the C<T1> access function?

http://bytes.com/topic/c/insights/65...erns-singleton
Jun 14 '13 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Christophe Barbe | last post by:
I posted a few days ago about the same problem but was not very clear. So here is my second take at it. Basically with GCC 3.3.2, I can't compile the example from the C++ FAQ Lite available...
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: Snyke | last post by:
Hi. I'm just wondering what friend classes/functions are. It's because I stumbled across them while analyzing a source.
4
by: frs | last post by:
How can I get a template friend? I want to achieve something like the subsequent two cases: (1) class A { template <typename T> friend class T; }; (2) template <typename T> class A {
2
by: Colin Fine | last post by:
Now that PHP5 has proper scoping for class properties and methods, I've been looking in vain for friend classes, or wondering if there's another way to achieve this effect: class A { ...
2
by: PengYu.UT | last post by:
The following program works with g++3.3. I'm assuming g++-3.4 is more standard conforming that g++-3.3. Would you please tell me what is right way to declare a template friend function of a...
3
by: Pierre Barbier de Reuille | last post by:
Hi, after reading the article " The Standard Librarian : Defining Iterators and Const Iterators" from Matt Austern:...
1
by: curious2007 | last post by:
Hello everybody, When I try to declare nontemplate friend functions I get the following warning: friend decleration......declares a non-template function (if this is not what you intended,...
1
by: Ralf Goertz | last post by:
Victor Bazarov wrote: Yes, you're right, they don't. At least not with my compiler. Is there a reason for this? I also tried to use a forward declaration of Derived2 before the definition of...
1
by: farseerfc | last post by:
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: ...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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,...
1
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...
1
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.