🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.sys.mac.programmer.codewarrior
5 messages
5 total messages Started by Adin Hunter Baber Mon, 19 Nov 2001 20:13
Problem with friendly templates
#1114
Author: Adin Hunter Baber
Date: Mon, 19 Nov 2001 20:13
28 lines
2028 bytes
I was told that if you want to have a template type be friends with one 
another you need to declare it like this:

template<class T>
class temClass
   {
   public:
      ...
      template<class U> friend class temClass<U>;
   ...
   };

However, if i write my code this way I get a "declaration syntax error" 
and the Error & Warnings Window has friend underlined.

One work around, that appears to work fine, is have the templates all 
derive from a common base class.  The base class contains the data that 
needs to be shared. When the sharing of data is necessary, one of the 
classes is upcast, and the appropriate data is accessed.

Current system:
Mac OS9.1 Dual 533 640 Meg RAM.

-- 
Adin Hunter Baber
"Death is lighter than a feather, Duty heavier than a mountain"
"...nevertheless, they fought on even though their homes were piles of ashes,
even though they knew they could not win they still fought on,..."
Re: Problem with friendly templates
#1090
Author: David 'Potatoswatter' Krauss
Date: Tue, 20 Nov 2001 04:32
65 lines
3547 bytes
[[ This message was both posted and mailed: see
   the "To," "Cc," and "Newsgroups" headers for details. ]]

In article
<mjolnir_DELETE_-279068.14135819112001@east.usenetserver.com>, Adin
Hunter Baber <mjolnir_DELETE_@soltec.net> wrote:

> I was told that if you want to have a template type be friends with one 
> another you need to declare it like this:
> 
> template<class T>
> class temClass
>    {
>    public:
>       ...
>       template<class U> friend class temClass<U>;
>    ...
>    };
> 
> However, if i write my code this way I get a "declaration syntax error" 
> and the Error & Warnings Window has friend underlined.
 
Nah - I don't see how this could work. The basic principle behind
templates is that they have to be instantiated before anything is
actually declared. "friend" statements are more directives than
declarations - you can't instantiate them.

> One work around, that appears to work fine, is have the templates all 
> derive from a common base class.  The base class contains the data that 
> needs to be shared. When the sharing of data is necessary, one of the 
> classes is upcast, and the appropriate data is accessed.

This is always a good idea. Putting as much functionality as possible
into base classes optimizes your programs, makes debugging easier, and
makes the gods love you more ;v) . Ideally, templates should only be
"wrappers" that declare no non-inline functions.

   I'm a bit confused about your situation, tho. You need to call
across to members of other instantiations, from within members of the
template class. Don't you necessarily know which other template
instantiations you're calling across to? I'd think this would be
necessary to instantiate the calling template in the first place. In
that case, the type of the called instance (or the parameters to that
template) can be a template parameter to the caller.

template< class Operand, class Helper >
class TemClass {
public:
   friend class TemClass< Helper >;
private:
   void Method( Operand arg );
};


template< class Operand, class Helper >
void TemClass::Method( Operand arg ) {
   TemClass<UInt32, Helper> grr; // this is the only way to call
template instance instances
   grr.Method( 5 );
}

Code for illustrative purposes only ;v)

   - hth,
   Potatoswatter
Re: Problem with friendly templates
#1091
Author: meeroh
Date: Tue, 20 Nov 2001 05:13
66 lines
3216 bytes
In article <191120012332200672%potswa@mac.nospam.com>,
 David 'Potatoswatter' Krauss <potswa@mac.nospam.com> wrote:

>[[ This message was both posted and mailed: see
>   the "To," "Cc," and "Newsgroups" headers for details. ]]
>
>In article
><mjolnir_DELETE_-279068.14135819112001@east.usenetserver.com>, Adin
>Hunter Baber <mjolnir_DELETE_@soltec.net> wrote:
>
>> I was told that if you want to have a template type be friends with one 
>> another you need to declare it like this:
>> 
>> template<class T>
>> class temClass
>>    {
>>    public:
>>       ...
>>       template<class U> friend class temClass<U>;
>>    ...
>>    };
>> 
>> However, if i write my code this way I get a "declaration syntax error" 
>> and the Error & Warnings Window has friend underlined.
> 
>Nah - I don't see how this could work. The basic principle behind
>templates is that they have to be instantiated before anything is
>actually declared. "friend" statements are more directives than
>declarations - you can't instantiate them.

It can work rather simply.

template <class T>
class c1 {
   // blah blah
};

template <class T>
class c2 {
   template <class U> friend class c1 <U>;
};

except that CWP7 doesn't support friend templates yet, which is why this 
code won't compile.

>> One work around, that appears to work fine, is have the templates all 
>> derive from a common base class.  The base class contains the data that 
>> needs to be shared. When the sharing of data is necessary, one of the 
>> classes is upcast, and the appropriate data is accessed.
>
>This is always a good idea. Putting as much functionality as possible
>into base classes optimizes your programs, makes debugging easier, and
>makes the gods love you more ;v) . Ideally, templates should only be
>"wrappers" that declare no non-inline functions.

I think you are grossly over-generalizing. There are many cases when 
it's very wrong to put as much functionality as possible into base 
classes, and the answer to such questions is highly dependent on the 
problem domain.

(and since we are given no information about the problem domain, I don't 
have much useful to say...)

meeroh
-- 
Hire me: <http://meeroh.org/hire.html>
Re: Problem with friendly templates
#1092
Author: Russell Zornes
Date: Tue, 20 Nov 2001 06:06
22 lines
1679 bytes
in article mjolnir_DELETE_-279068.14135819112001@east.usenetserver.com, Adin
Hunter Baber at mjolnir_DELETE_@soltec.net wrote on 11/19/01 12:13 PM:

> I was told that if you want to have a template type be friends with one
> another you need to declare it like this:
> 
> template<class T>
> class temClass
>  {
>  public:
>     ...
>     template<class U> friend class temClass<U>;
>  ...
>  };
> 
> However, if i write my code this way I get a "declaration syntax error"
> and the Error & Warnings Window has friend underlined.

Your syntax for a template friend is correct but there is a bug in CW that
prevents it from compiling. I think Ron said this will be fixed in Pro 8.

-Russ
Re: Problem with friendly templates
#1093
Author: Adin Hunter Baber
Date: Tue, 20 Nov 2001 23:06
34 lines
2521 bytes
Thanks for the info everyone.  i was rather suspicious that the problem 
with friendly templates was a CW error.

As for not posting code, I just really wanted to see if the whole thing 
was a CW error or not.  Sorry about not making the question clear.

The stuff I am working on (with what little free time I have) are some 
smart pointers that support polymorphism.  I think my design is pretty 
much laid down, I am just at a phase where I am verifying my assumptions.

Ironically, CW's error may have helped the design.  By putting operators 
==, !=, and bool into a base class, I don't have to worry about two 
different (incompatible) pointers being compared, and getting a true 
result.

Ex:
SmartPointer<int> ip = etc
SmartPointer<SomeClass> scp = etc

if(ip == scp)
// will return true since both types are converted to bools, 
// and then compared

by putting operator ==, != into a base class, I mostly avoid the problem
if(ip == scp)
// will automatically convert both types to the smart base class
// and see if they really point to the same place.  Thus we get
// correct results.

-- 
Adin Hunter Baber
"Death is lighter than a feather, Duty heavier than a mountain"
"...nevertheless, they fought on even though their homes were piles of ashes,
even though they knew they could not win they still fought on,..."
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads