By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,290 Members | 1,666 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,290 IT Pros & Developers. It's quick & easy.

using friend and template class

P: n/a
Hi,

is a construction like the following possible:

template<class view_model>
class template_clase
{
protected:
template_clase() {}
virtual ~template_clase() {}
};

clase friend_class
{
friend_class() {}
virtual ~friend_class{}

friend template_class;
};

My compile ( Visual C++ 7.0 ) doesn't accept this code. Is it possible and
if yes how, to use a template class as a friend class?

Thanks in advance
Sebastian
Jul 19 '05 #1
Share this Question
Share on Google+
21 Replies


P: n/a
WW
Sebastian Faust wrote:
Hi,

is a construction like the following possible:

template<class view_model>
class template_clase
{
protected:
template_clase() {}
virtual ~template_clase() {}
};

clase friend_class
{
friend_class() {}
virtual ~friend_class{}

friend template_class;
};

My compile ( Visual C++ 7.0 ) doesn't accept this code. Is it
possible and if yes how, to use a template class as a friend class?


There is no such thing as template class. There are class templates. Read
this two sentences until you grasp the difference. Class templates are
*only* templates. They are not classes. So they cannot be made friends.

Furthermore I do not understand that why do you call the class giving friend
access friend_class? In C++ we call friend class the class *being given*
the friendship. Furthermore you have template_clase and template_class. I
assume this is a typo.

--
WW aka Attila
Jul 19 '05 #2

P: n/a
Hi,

This code is works (VC++ 7.1):
template<class view_model> class template_clase

{

protected:

template_clase() {}

virtual ~template_clase() {}

};

class friend_class

{

friend_class() {}

virtual ~friend_class() {}

friend class template_clase<class view_model>; // this is a
template!!!!

};

:-))

Have fun,

Viatcheslav.

"Sebastian Faust" <sf*************@logic-software.de> wrote in message
news:bl*************@news.t-online.com...
Hi,

is a construction like the following possible:

template<class view_model>
class template_clase
{
protected:
template_clase() {}
virtual ~template_clase() {}
};

clase friend_class
{
friend_class() {}
virtual ~friend_class{}

friend template_class;
};

My compile ( Visual C++ 7.0 ) doesn't accept this code. Is it possible and
if yes how, to use a template class as a friend class?

Thanks in advance
Sebastian

Jul 19 '05 #3

P: n/a

"WW" <wo***@freemail.hu> wrote in message
news:bl**********@phys-news1.kolumbus.fi...
Sebastian Faust wrote:
Hi,

is a construction like the following possible:

template<class view_model>
class template_clase
{
protected:
template_clase() {}
virtual ~template_clase() {}
};

clase friend_class
{
friend_class() {}
virtual ~friend_class{}

friend template_class;
};

My compile ( Visual C++ 7.0 ) doesn't accept this code. Is it
possible and if yes how, to use a template class as a friend class?
There is no such thing as template class. There are class templates.

Read this two sentences until you grasp the difference. Class templates are
*only* templates. They are not classes. So they cannot be made friends.
1. At least at definition we call it "template <typename X> class Y". :-))
2. Both "template classes" and "classes" are data abstractions.
3. Both abstractions has visibility rules (public, protected, private) for
data/methods.
4. For some other abstractions (does not matter - "template classes",
"classes", "functions") we can allow to neglect visibility rules. (This is
what "friend"(s) are for :-)) )

5. Therefore: there is no logical reason that template classes can not made
friends.

Viatcheslav.

Furthermore I do not understand that why do you call the class giving friend access friend_class? In C++ we call friend class the class *being given*
the friendship. Furthermore you have template_clase and template_class. I assume this is a typo.

--
WW aka Attila

Jul 19 '05 #4

P: n/a
WW
Viatcheslav L. Gorelenkov wrote:
"WW" <wo***@freemail.hu> wrote in message
There is no such thing as template class. There are class
templates. Read this two sentences until you grasp the difference.
Class templates are *only* templates. They are not classes. So
they cannot be made friends.
1. At least at definition we call it "template <typename X> class Y".
:-))


Haha
2. Both "template classes" and "classes" are data abstractions.
There is no such thing as template class.
3. Both abstractions has visibility rules (public, protected,
private) for data/methods.
Class templates has none. Class template instances (which are classes)
have.
4. For some other abstractions (does not matter - "template classes",
"classes", "functions") we can allow to neglect visibility rules.
(This is what "friend"(s) are for :-)) )
There is no such thing as template class.
5. Therefore: there is no logical reason that template classes can
not made friends.

Except that there is no such thing as template class.

Only class templates exist.

--
WW aka Attila
Jul 19 '05 #5

P: n/a
WW
Viatcheslav L. Gorelenkov wrote:
[SNIP]
friend class template_clase<class view_model>; // this is a
template!!!!


Wrong again. The above is *not* a template. It is a concrete type, a
concrete instance of a class template. For newbies: this is a beautiful way
of how *not* to learn anything. With trial and error, not even knowing what
a class template is, trying to insert and remove things until it works - and
finally still *not knowing anything* and state that a concrete class is a
template.

If you ever decide to learn anything do it right. Learn what the
terminology is, because that defines already a lot. And if you did not
learn it and you are warned about using the worng word(s) here: think about
it. Unless you want to make a fool of yourself as the above person did.

--
WW aka Attila
Jul 19 '05 #6

P: n/a

"WW" <wo***@freemail.hu> wrote in message
news:bl**********@phys-news1.kolumbus.fi...
Viatcheslav L. Gorelenkov wrote:
[SNIP]
friend class template_clase<class view_model>; // this is a
template!!!!
Wrong again. The above is *not* a template. It is a concrete type, a
concrete instance of a class template. For newbies: this is a beautiful

way of how *not* to learn anything. With trial and error, not even knowing what a class template is, trying to insert and remove things until it works - and finally still *not knowing anything* and state that a concrete class is a
template.
1. The above IS template. This is NOT a concreate instance. Take a look what
is inside angle brackets ( <..> ).
If you ever decide to learn anything do it right.
2. Completely agree.

Viatcheslav.
Learn what the
terminology is, because that defines already a lot. And if you did not
learn it and you are warned about using the worng word(s) here: think about it. Unless you want to make a fool of yourself as the above person did.

--
WW aka Attila

Jul 19 '05 #7

P: n/a
WW
Viatcheslav L. Gorelenkov wrote:
"WW" <wo***@freemail.hu> wrote in message
news:bl**********@phys-news1.kolumbus.fi...
Viatcheslav L. Gorelenkov wrote:
[SNIP]
friend class template_clase<class view_model>; // this is a
template!!!!


Wrong again. The above is *not* a template. It is a concrete type,
a concrete instance of a class template. For newbies: this is a
beautiful way of how *not* to learn anything. With trial and error,
not even knowing what a class template is, trying to insert and
remove things until it works - and finally still *not knowing
anything* and state that a concrete class is a template.

1. The above IS template. This is NOT a concreate instance. Take a
look what is inside angle brackets ( <..> ).


I have missed the class keyword there. Blame it on my decomposing mind.
Unfortunately the aboveis not yet C++, but it is proposed!

I thought you wanted to make one *instance* of that template a friend, the
one instantiated from friend_class. I guess I have to work harder on
eliminating my preconceptions when reading a post. And especially when
answering to it. :-(
If you ever decide to learn anything do it right.


2. Completely agree.


The "template friend" you are looking for it (right now) not part of C++.
AFAIK there is a proposal to add it - but I cannot find it.

--
WW aka Attila
Jul 19 '05 #8

P: n/a
"Sebastian Faust" <sf*************@logic-software.de> wrote in message news:<bl*************@news.t-online.com>...
template<class view_model>
class template_clase
{
protected:
template_clase() {}
virtual ~template_clase() {}
};

clase friend_class
{
friend_class() {}
virtual ~friend_class{}

WRONG:
friend template_class;
RIGHT:

template <class> friend class template_class;

It's a template, so tell that to the compiler. Also, the keyword
"class" is required after "friend" if it's a class or class template.
};

My compile ( Visual C++ 7.0 ) doesn't accept this code. Is it possible and
if yes how, to use a template class as a friend class?


Visual C++ 7.0 does allow the above code. (6.0, however, does not; it
does not allow template friends at all.)

- Shane
Jul 19 '05 #9

P: n/a
WW
WW wrote:
[SNIP my junk]

I have to sleep more. Or less. I have been lost in the timeline, back to
1995. Of course you can make all instances of a class template friend. See
Shane's post.

--
WW aka Attila
Shaving hair off again, preparing ash
Jul 19 '05 #10

P: n/a
"Viatcheslav L. Gorelenkov" <vl****@boo.net> wrote in message news:<bl**********@news.chatlink.com>...
friend class template_clase<class view_model>; // this is a
template!!!!


Nope. You're giving friendship to template_clase<view_model>, where
view_model is a class, not template_clase itself. This is similar to
the following:

template <typename T> class foo {
public:
typedef typename T::type type;
};

class bar {
private:
typedef void type;
// give friendship to foo<bar>, where bar is a class
friend class foo<bar>;
};

// explicitly instantiate foo<bar> (for testing purposes)
template class foo<bar>;

Only foo<bar> is friends with foo, not foo<int>, etc. If you want all
instantiations of foo to be given friendship, do as thus:

template <class> friend class foo;

This makes friends with foo, which is a class template which takes one
parameter.

- Shane
Jul 19 '05 #11

P: n/a
WW
Shane Beasley wrote:
[SNIP]
template <class> friend class foo;

This makes friends with foo, which is a class template which takes one
parameter.


Yep. (Hopefully) temporary insanity from my side.

--
WW aka Attila
Jul 19 '05 #12

P: n/a

"Shane Beasley" <sb******@cs.uic.edu> wrote in message
news:2f**************************@posting.google.c om...
"Viatcheslav L. Gorelenkov" <vl****@boo.net> wrote in message news:<bl**********@news.chatlink.com>...
friend class template_clase<class view_model>; // this is a
template!!!!


1. This is a template. ( !!!!! :-))) ) See source code in my 1-st posting. I
do not give friendship to instantiated template_clase<view_model>, rather it
is friendship to class template_clase<class view_model>. See what is between
the angle brackets <...>. Your can change view_model to C, T, whatever - it
still will compile and work the same way.

Nope. You're giving friendship to template_clase<view_model>, where
view_model is a class, not template_clase itself. This is similar to
the following:

template <typename T> class foo {
public:
typedef typename T::type type;
};

class bar {
private:
typedef void type;
// give friendship to foo<bar>, where bar is a class
friend class foo<bar>;
2. To give friendship to template class foo :
friend class foo<class TTT>; // CLASS TTT

This is true for MS VC++ compiler ( see original posting).

Viatcheslav.

};

// explicitly instantiate foo<bar> (for testing purposes)
template class foo<bar>;

Only foo<bar> is friends with foo, not foo<int>, etc. If you want all
instantiations of foo to be given friendship, do as thus:

template <class> friend class foo;

This makes friends with foo, which is a class template which takes one
parameter.

- Shane

Jul 19 '05 #13

P: n/a
"Viatcheslav L. Gorelenkov" <vl****@boo.net> wrote in message news:<bl**********@news.chatlink.com>...
friend class template_clase<class view_model>; // this is a
template!!!!


1. This is a template. ( !!!!! :-))) ) See source code in my 1-st posting.


I thought the above code was yours... Did I copy it incorrectly?
I
do not give friendship to instantiated template_clase<view_model>, rather it
is friendship to class template_clase<class view_model>. See what is between
the angle brackets <...>. Your can change view_model to C, T, whatever - it
still will compile and work the same way.
Completely and utterly wrong. Observe:

template <class> class template_clase;

class friend_class {
private:
static void f () { }

#ifdef YOURCODE
friend class template_clase<class view_model>;
#else
template <class> friend class template_clase;
#endif
};

template <class view_model>
class template_clase {
public:
void f () { friend_class::f(); } // 18
};

class view_model { };
template class template_clase<view_model>;
template class template_clase<friend_class>; // 23

If YOURCODE is defined, Comeau gives me the following:

"ComeauTest.c", line 18: error: function "<unnamed>::friend_class::f"
is inaccessible
void f () { friend_class::f(); } // 18
^
detected during instantiation of "void
<unnamed>::template_clase<view_model>::f() [with
view_model=<unnamed>::friend_class]" at line 23

In other words, template_clase<view_model> has access to
friend_class::f(), but not template_clase<friend_class>. Therefore,
your code gives friendship only to template_clase<view_model>.

If YOURCODE is *not* defined, Comeau accepts the code. That means that
only my code gives friendship to both template_clase<view_model> and
template_clase<friend_class> -- in fact, it gives friendship to all
instantiations of template_clase.
2. To give friendship to template class foo :
friend class foo<class TTT>; // CLASS TTT

This is true for MS VC++ compiler ( see original posting).


No, it is not. Visual C++ .NET (aka 7.0) gives friendship as ISO C++,
Comeau, GCC 3.2, and I say it should. It accepts (and rejects) the
above code exactly as all these compilers do.

(NB: Visual C++ 6.0 is broken and cannot give friendship to templates.
It rejects the above code regardless of whether YOURCODE is defined.)

- Shane
Jul 19 '05 #14

P: n/a
WW wrote:

Viatcheslav L. Gorelenkov wrote:
"WW" <wo***@freemail.hu> wrote in message
There is no such thing as template class. There are class
templates. Read this two sentences until you grasp the difference.
Class templates are *only* templates. They are not classes. So
they cannot be made friends.


1. At least at definition we call it "template <typename X> class Y".
:-))


Haha
2. Both "template classes" and "classes" are data abstractions.


There is no such thing as template class.


A class template is a pattern for a class. A template class is an
instantiation of a class template. Read the FAQ!

/david

--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
-- unknown
Jul 19 '05 #15

P: n/a
WW
David Rubin wrote:
[SNIPO]
There is no such thing as template class.


A class template is a pattern for a class. A template class is an
instantiation of a class template. Read the FAQ!


I read the standard, than you. There is no such thing as template class.

--
WW aka Attila
Jul 19 '05 #16

P: n/a
WW wrote:

David Rubin wrote:
[SNIPO]
There is no such thing as template class.


A class template is a pattern for a class. A template class is an
instantiation of a class template. Read the FAQ!


I read the standard, than you. There is no such thing as template class.


In that case, you should send a correction to the FAQ maintainer:

http://www.parashift.com/c++-faq-lit....html#faq-34.7
"Unlike template functions, template classes (instantiations of class
templates)..."

/david

--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
-- unknown
Jul 19 '05 #17

P: n/a
WW
David Rubin wrote:
WW wrote:

David Rubin wrote:
[SNIPO]
There is no such thing as template class.

A class template is a pattern for a class. A template class is an
instantiation of a class template. Read the FAQ!
I read the standard, than you. There is no such thing as template
class.


In that case, you should send a correction to the FAQ maintainer:

http://www.parashift.com/c++-faq-lit....html#faq-34.7 "Unlike template functions, template classes (instantiations of class
templates)..."


Again. Read the standard. Or read C++ TEMPLATES, The Complete Guide by
David Vandevoorde & Nicolai M. Josuttis ISBN 0-201-73484-2 on Page 87,
Chapter 7, Basic Template Terminology.""Because of this imprecision, we
avoid the term /template/ /class/ on this book." Before that it list 3,
ancient, all dropped meanings for the "term" (I whish he had used phrase).
Also those who followed the making of the standard also know that term
template class has been dropped all over the standard - because it was an
incorrecmt one. There are (class) template instances. There are template
IDs. There is partial specialization and many many more things. There is
now such things as template class.

--
WW aka Attila
Jul 19 '05 #18

P: n/a
WW wrote:
David Rubin wrote:
WW wrote:
David Rubin wrote:
[SNIPO]

>There is no such thing as template class.

A class template is a pattern for a class. A template class is an
instantiation of a class template. Read the FAQ!

I read the standard, than you. There is no such thing as template
class.


In that case, you should send a correction to the FAQ maintainer:


http://www.parashift.com/c++-faq-lit....html#faq-34.7
"Unlike template functions, template classes (instantiations of class
templates)..."

Again. Read the standard. Or read C++ TEMPLATES, The Complete Guide by
David Vandevoorde & Nicolai M. Josuttis ISBN 0-201-73484-2 on Page 87,
Chapter 7, Basic Template Terminology.""Because of this imprecision, we
avoid the term /template/ /class/ on this book." Before that it list 3,
ancient, all dropped meanings for the "term" (I whish he had used phrase).
Also those who followed the making of the standard also know that term
template class has been dropped all over the standard - because it was an
incorrecmt one. There are (class) template instances. There are template
IDs. There is partial specialization and many many more things. There is
now such things as template class.


This is pretty interesting, and I'll take you at your word that the term
"template class" is deprecated. However, as a clc++ regular (you seem to be), I
thought that you might have some interest in correcting the FAQ since everyone
knows to look at the FAQ, whereas not many people would pick up Vandevoorde and
Josuttis.

/david

--
FORTRAN was the language of choice
for the same reason that three-legged races are popular.
-- Ken Thompson, "Reflections on Trusting Trust"

Jul 19 '05 #19

P: n/a
WW
David Rubin wrote:
[SNIPPODROM]
This is pretty interesting, and I'll take you at your word that the
term "template class" is deprecated.
It isn't. It has never been in the standard.
However, as a clc++ regular (you
seem to be), I thought that you might have some interest in
correcting the FAQ since everyone knows to look at the FAQ, whereas
not many people would pick up Vandevoorde and Josuttis.


Yeah. But it is also known that the one who wrote the FAQ is another man
with a strong opinion. And his definition is one of the three possible. :-)
So IMO I would have a reaaaaly hard time to convince him. But I can give it
a try. :-) But I have to prepare for it. ;-)

--
WW aka Attila
Jul 19 '05 #20

P: n/a
The problem here is that template_class is not a class - it is a
template for creating classes. You can do something like:

friend template_class<foo>;

to make the template_class<foo> class a friend, but
template_class<bar> will not be a friend. To do what you're looking
for would require an extention to the C++ language so that you could
write something like:

template <typename T> friend template_class<T>;

Unfortunatly, this construct simply doesn't exist. I would not be at
all suprised if the designers have a very good reason for this,
although I haven't taken the time to look into it.

"Sebastian Faust" <sf*************@logic-software.de> wrote in message news:<bl*************@news.t-online.com>...
Hi,

is a construction like the following possible:

template<class view_model>
class template_clase
{
protected:
template_clase() {}
virtual ~template_clase() {}
};

clase friend_class
{
friend_class() {}
virtual ~friend_class{}

friend template_class;
};

My compile ( Visual C++ 7.0 ) doesn't accept this code. Is it possible and
if yes how, to use a template class as a friend class?

Thanks in advance
Sebastian

Jul 19 '05 #21

P: n/a
dslater wrote:
The problem here is...


Please don't top-post. Read section 5 of the FAQ for posting guidelines.

http://www.parashift.com/c++-faq-lite/

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #22

This discussion thread is closed

Replies have been disabled for this discussion.