🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.sys.mac.programmer.codewarrior
Article #1090

Re: Problem with friendly templates

#1090
From: 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
Technical Details
Message-ID: <191120012332200672%potswa@mac.nospam.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!cyclone2.usenetserver.com!usenetserver.com!news01.optonline.net!news02.optonline.net.POSTED!not-for-mail
References: <mjolnir_DELETE_-279068.14135819112001@east.usenetserver.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10bcce,45060c4a2ff696d4
X-Google-Attributes: gid10bcce,public
X-Google-Thread: f635e,45060c4a2ff696d4
X-Google-Attributes: gidf635e,public
X-Google-ArrivalTime: 2001-11-19 20:37:17 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!cyclone2.usenetserver.com!usenetserver.com!news01.optonline.net!news02.optonline.net.POSTED!not-for-mail
Subject: Re: Problem with friendly templates
From: David 'Potatoswatter' Krauss <potswa@mac.nospam.com>
Newsgroups: comp.sys.mac.programmer.codewarrior,codewarrior.mac
Posted-And-Mailed: yes
Message-ID: <191120012332200672%potswa@mac.nospam.com>
References: <mjolnir_DELETE_-279068.14135819112001@east.usenetserver.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-transfer-encoding: 8bit
Mail-Copies-To: nobody
User-Agent: YA-NewsWatcher/5.0.1
Lines: 65
Date: Tue, 20 Nov 2001 04:32:25 GMT
NNTP-Posting-Host: 24.191.2.132
X-Trace: news02.optonline.net 1006230745 24.191.2.132 (Mon, 19 Nov 2001 23:32:25 EST)
NNTP-Posting-Date: Mon, 19 Nov 2001 23:32:25 EST
Organization: Optimum Online