using friend and template class

S

Sebastian Faust

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
 
W

WW

Sebastian said:
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.
 
V

Viatcheslav L. Gorelenkov

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.
 
V

Viatcheslav L. Gorelenkov

WW said:
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.
 
W

WW

Viatcheslav said:
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.
 
W

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.
 
V

Viatcheslav L. Gorelenkov

WW said:
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 ( said:
If you ever decide to learn anything do it right.

2. Completely agree.

Viatcheslav.
 
W

WW

Viatcheslav said:
WW said:
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. :-(
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.
 
S

Shane Beasley

Sebastian Faust said:
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
 
W

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.
 
S

Shane Beasley

Viatcheslav L. Gorelenkov said:
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
 
W

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.
 
V

Viatcheslav L. Gorelenkov

Shane Beasley said:
"Viatcheslav L. Gorelenkov" <[email protected]> wrote in message

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.
 
S

Shane Beasley

Viatcheslav L. Gorelenkov said:
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
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
 
D

David Rubin

WW said:
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
 
W

WW

David Rubin wrote:
[SNIPO]
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.
 
D

David Rubin

WW said:
David Rubin wrote:
[SNIPO]
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-lite/containers-and-templates.html#faq-34.7
"Unlike template functions, template classes (instantiations of class
templates)..."

/david
 
W

WW

David said:
WW said:
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-lite/containers-and-templates.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.
 
D

David Rubin

WW said:
David said:
WW said:
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-lite/containers-and-templates.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
 
W

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. ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top