C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

using friend and template class
Goto page 1, 2  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Sebastian Faust
Guest





PostPosted: Sat Sep 27, 2003 7:49 pm    Post subject: using friend and template class Reply with quote



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


Back to top
WW
Guest





PostPosted: Sat Sep 27, 2003 8:50 pm    Post subject: Re: using friend and template class Reply with quote



Sebastian Faust wrote:
Quote:
Hi,

is a construction like the following possible:

template 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



Back to top
Viatcheslav L. Gorelenkov
Guest





PostPosted: Sun Sep 28, 2003 1:53 am    Post subject: Re: using friend and template class Reply with quote



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" <sfaust_deleteme (AT) logic-software (DOT) de> wrote

Quote:
Hi,

is a construction like the following possible:

template 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





Back to top
Viatcheslav L. Gorelenkov
Guest





PostPosted: Sun Sep 28, 2003 2:28 am    Post subject: Re: using friend and template class Reply with quote


"WW" <wolof (AT) freemail (DOT) hu> wrote

Quote:
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 )
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 Smile) )

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

Viatcheslav.



Quote:

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





Back to top
WW
Guest





PostPosted: Sun Sep 28, 2003 7:30 am    Post subject: Re: using friend and template class Reply with quote

Viatcheslav L. Gorelenkov wrote:
Quote:
"WW" <wolof (AT) freemail (DOT) 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".
Smile)

Haha

Quote:
2. Both "template classes" and "classes" are data abstractions.

There is no such thing as template class.

Quote:
3. Both abstractions has visibility rules (public, protected,
private) for data/methods.

Class templates has none. Class template instances (which are classes)
have.

Quote:
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 Smile) )

There is no such thing as template class.

Quote:
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



Back to top
WW
Guest





PostPosted: Sun Sep 28, 2003 7:35 am    Post subject: Re: using friend and template class Reply with quote

Viatcheslav L. Gorelenkov wrote:
[SNIP]
Quote:
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



Back to top
Viatcheslav L. Gorelenkov
Guest





PostPosted: Sun Sep 28, 2003 2:04 pm    Post subject: Re: using friend and template class Reply with quote


"WW" <wolof (AT) freemail (DOT) hu> wrote

Quote:
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 ( <..> ).

Quote:
If you ever decide to learn anything do it right.

2. Completely agree.

Viatcheslav.

Quote:
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





Back to top
WW
Guest





PostPosted: Sun Sep 28, 2003 5:07 pm    Post subject: Re: using friend and template class Reply with quote

Viatcheslav L. Gorelenkov wrote:
Quote:
"WW" <wolof (AT) freemail (DOT) hu> wrote in message
news:bl6301$nfc$1 (AT) phys-news1 (DOT) 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. :-(

Quote:
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



Back to top
Shane Beasley
Guest





PostPosted: Sun Sep 28, 2003 6:28 pm    Post subject: Re: using friend and template class Reply with quote

"Sebastian Faust" <sfaust_deleteme (AT) logic-software (DOT) de> wrote


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

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


WRONG:

Quote:
friend template_class;

RIGHT:

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

Quote:
};

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

Back to top
WW
Guest





PostPosted: Sun Sep 28, 2003 6:52 pm    Post subject: Re: using friend and template class Reply with quote

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


Back to top
Shane Beasley
Guest





PostPosted: Sun Sep 28, 2003 7:02 pm    Post subject: Re: using friend and template class Reply with quote

"Viatcheslav L. Gorelenkov" <vlg315 (AT) boo (DOT) net> wrote


Quote:
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

Back to top
WW
Guest





PostPosted: Sun Sep 28, 2003 7:04 pm    Post subject: Re: using friend and template class Reply with quote

Shane Beasley wrote:
[SNIP]
Quote:
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



Back to top
Viatcheslav L. Gorelenkov
Guest





PostPosted: Sun Sep 28, 2003 9:37 pm    Post subject: Re: using friend and template class Reply with quote


"Shane Beasley" <sbeasley (AT) cs (DOT) uic.edu> wrote

Quote:
"Viatcheslav L. Gorelenkov" <vlg315 (AT) boo (DOT) net> wrote


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


1. This is a template. ( !!!!! Smile)) ) 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.


Quote:
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.


Quote:
};

// 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



Back to top
Shane Beasley
Guest





PostPosted: Mon Sep 29, 2003 3:25 am    Post subject: Re: using friend and template class Reply with quote

"Viatcheslav L. Gorelenkov" <vlg315 (AT) boo (DOT) net> wrote


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


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

I thought the above code was yours... Did I copy it incorrectly?

Quote:
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.

Quote:
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

Back to top
David Rubin
Guest





PostPosted: Mon Sep 29, 2003 5:47 pm    Post subject: Re: using friend and template class Reply with quote

WW wrote:
Quote:

Viatcheslav L. Gorelenkov wrote:
"WW" <wolof (AT) freemail (DOT) 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".
Smile)

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

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.