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

Multiple Template-Parameter Friends

I have a problem, which boils down to this:

Expand|Select|Wrap|Line Numbers
  1. template<typename T>
  2. class bar;
  3.  
  4. template<typename T, typename U>
  5. void foo(const U& u) {
  6.   bar<T> baz;
  7.   baz.m_member = 0;
  8. }
  9.  
  10. template<typename T>
  11. class bar
  12. {
  13.   // friend...
  14.  
  15. private:
  16.   int m_member;
  17. };
  18.  
  19. int main() {
  20.   foo<char>(1);
  21. }
  22.  
What friend declaration should go in the template class bar in order to grant friendship to the template function foo? Is that even possible, or do I have to write the definition of foo inside bar?
Oct 21 '07 #1
1 1615
weaknessforcats
9,208 Expert Mod 8TB
What friend declaration should go in the template class bar in order to grant friendship to the template function foo? Is that even possible, or do I have to write the definition of foo inside bar?
Yes, it's possible butr you need toi clearly understand hiow templates work. That is, you cannot have a function prototype be a template.

Therfore, you cannot have a friend function be a template that's outside the class template.

There are two cases:
1) Bound friend functions
2) Unbound friend functions.

A bound friend function can be a template function but not a function template.
That is, you can use an already specialized template:
Expand|Select|Wrap|Line Numbers
  1. template <class T>
  2. void BoundFriend(T arg)
  3. {
  4.  
  5. }
  6. template <class T>
  7. class MyClass
  8. {
  9.     public:
  10.     friend void BoundFriend(int);
  11. };
  12. int main()
  13. {
  14.     MyClass<double> obj;
  15. }
  16.  
The second case is the unbound friend. Here the type used in the friend is not known until the object is created. Therefore, the unbound friend will be whatever type was used to create the template class. Therefore, you cannot have a prototype of the friend inside the class template. Therefore, you must insert the entire friend function template inside the class template:
Expand|Select|Wrap|Line Numbers
  1. template <class T>
  2. class MyClass
  3. {
  4.     public:
  5.     template <class T>
  6.     friend void UnBoundFriend(T arg)
  7.     {
  8.  
  9.     }
  10. };
  11. int main()
  12. {
  13.     MyClass<double> obj;
  14. }
  15.  
Finally, I am picking my words carefully to avoid misunderstanding:
class template -> this is the template itself
template class -> the specialized template created from a copy of the class template.
Expand|Select|Wrap|Line Numbers
  1. template <class T>
  2. class MyClass                   //the class template
  3. {
  4.  
  5. };
  6. //
  7. int main()
  8. {
  9.      MyClass<int> obj;         //MyClass<int> is the template class.
  10. }
  11.  
The same is true with functions:
function template -> this is the template itself
template function -> the specialized template created from a copy of the function template.
Oct 21 '07 #2

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

Similar topics

6
by: Paul | last post by:
In real life situation, do we ever come across a situation where we would need two base objects in an object. A snippet is worth 1000 words (: so... class Base { }; class Derived1:public Base...
2
by: Hartmut Sbosny | last post by:
Hello NG, I have a question. I have a header file with a function template and a fully specialized version of it, for instance //======== File "templ.hpp" ======== #include <iostream> // the...
2
by: Cali | last post by:
Please bear with me, I have been reading about XSL for a couple hours. I have an XML document that contains multiple <page> tags interspersed throughout the tree. <text> .... <page>1</page>...
0
by: bouton | last post by:
I am trying to 'union' multiple xml files into one html table with only a few of the many fields/elements displayed using xsl. I have index.xml which lists the documents, all of which validate...
9
by: WL | last post by:
Hey, all. I'm creating an array of strings (char **argv style) on the fly, and using realloc to create string pointers, and malloc for the strings itself (if that makes any sense). I'm using the...
7
by: Dave | last post by:
Apologies for the newbie question. I have created a vb.net program for my company that is designed to work with Word Templates (about forty of them that we commonly use) that are selected by the...
11
by: Ranginald | last post by:
This question is about how to handle an .aspx page that references multiple methods and where to store these methods (e.g. one codefile or multiple codefiles). PREFACE ======== I have a simple...
14
by: Dave Rahardja | last post by:
Hi all, Is it possible to prevent _at compile or link time_ the mulitple instantiation of a templated function? In other words, if there exists a function template <typename Tvoid fn(); I...
2
by: Hel | last post by:
Hi, I'm sure you are familiar with this problem: A.h: template <unsigned Nstruct A { static const unsigned a; };
482
by: bonneylake | last post by:
Hey Everyone, Well i am not sure if this is more of a coldfusion problem or a javscript problem. So if i asked my question in the wrong section let me know an all move it to the correct place. ...
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: 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
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
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
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.