Re: OK in Visual C++6.0 but not in Visual C++ .NET 2003?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Carl Daniel [VC++ MVP] (cpdaniel_remove_this_and_nospam_at_mvps.org.nospam)
Date: 02/07/05


Date: Mon, 7 Feb 2005 06:39:29 -0800

Antti Keskinen wrote:
> Hello !
>
> A few more questions to help me clarify certain issues. In your
> previous post, you used the syntax
>
> friend void InitClass<T>(MyClass&);
>
> to declare all InitClass<T> instantations as friends of MyClass<T>
> (so that InitClass<U> is not a friend of MyClass<T>).

The instantion of the InitClass function template with template argument <T>
is a friend of the instantiation of the MyClass template with template
arguments <T>.

>
> However, the curious thing about this comes from the parameter list.
> If I had rewritten the friend declaration inside the class as
>
> friend void InitClass<T>(MyClass<T>& refClass);

They're equivalent. Within the definition of a class template, the name of
the template without a template argument list is synonymous with the fully
specified name. i.e. MyClass and MyClass<T> are the same.

> then how would this change the situation of who's a friend and who's
> not ? Or is it just a "syntax error" situation ? Or is the <T>
> unrequired in the parameter list because we are not talking about
> template declaration, but an instantation of it, as in, the <T> gets
> "copied" into the place where the template definition specifies it
> should go ?

Exactly.

>
> Moreover, when writing the following:
>
> template<typename T>
> class MyClass;
>
> template<typename T>
> void InitClass(MyClass<T>& refClass)
> {
> }
>
> template<typename T>
> class MyClass
> {
> template<typename T> friend void InitClass(MyClass<T>& refClass);
> };
>
> Does the 'friend' keyword ensure that InitClass doesn't become some
> sort of a nested-template declaration ? So this syntax means that all
> InitClass instantations are friends of MyClass<T>, meaning, an
> InitClass<int> can access MyClass<double>'s private/protected members
> ?

Correct.

>
> Also, if using a syntax like above, is it necessary to
> forward-declare the MyClass<T> template before the function template
> ? Like, if I used the "nested-template-looking" friend declaration of
> InitClass, I could move the actual InitClass<T> definition to after
> or before the class definition, and would not need any class
> declarations ?

It's necessary to forward-declare the class, otherwise it's impossible to
specifiy the parameter list for the function since MyClass is undefined at
that point.

>
> Again, thank you for answering these questions. Like you stated,
> templates are really one of the darkest corners of C++. But, very
> rewarding as well :D

You're welcome!

-cd



Relevant Pages

  • Friend function of a template class
    ... template class. ... MyFriendFunction (MyClass * const ptrMyClass) ... after class declaration. ...
    (comp.lang.cpp)
  • Re: Errors in VC program with gdiplus
    ... template argument list ... : see declaration of 'iterator' ...
    (microsoft.public.vc.mfc)
  • Errors in VC program with gdiplus
    ... template argument list ... : see declaration of 'iterator' ...
    (microsoft.public.vc.mfc)
  • Re: Proposal: Default Parameters/Named Parameters
    ... > Anders' mentioned the possibility of object initialization syntax that may ... MyClass obj = new MyClass ... which is also anders' opinion. ... > syntax to create a set of values that you pass to the constructor. ...
    (microsoft.public.dotnet.framework.clr)
  • Re: Long long int
    ... > more to the syntax of GAS or AT&T or whatever assembly. ... but that is simple absurdity. ... trained monkeys could have come up with a better template. ... algebraic notation is clearly a win over AT&T's What-The-Fuck notation. ...
    (comp.lang.c)