Re: friend class in template

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



Daniel wrote:
I have this header file that uses a template to create a class of a
sink object to handle connection points of a com object. There is a
line that makes a reference to class_event_handler. However, I don't
see that name anywhere else in the file. It doesn't seem to be used
for anything. Could someone tell me what the function of that line
is? I included a portion of the code below that includes that line. I
also attached the header file. Any help will be appreciated. This
file came from CodeProject by Lim Bio Liong. It works. I'm trying
to figure out the specific workings of it.

This template takes 3 type parameters, the first of which is named
event_handler. The friend declaration attempts to declare the class
event_handler to be a friend of this class, presumably to allow the class
actually specified for the first template parameter to have access to this
class's internals.

Unfortunately, the friend declaration doesn't do what the author apparently
thought it would do (at least not on a conforming compiler). This
declaration names a namespace scoped class named 'event_handler' as a
friend - the class actually bound to the formal template parameter named
'event_handler' is not befriended, as I suspect the author intended. Worse
than that, it won't even compile with a conforming compiler.

Here's a simple test case:

template <class T> class X
{
friend class T;

int x;
};

struct Y
{
void f(X<Y>& x) { x.x=1; }
};

void f()
{
X<Y> xy;
}

VC7.1 and VC8 both raise an error that Y::f cannot access private member
X<Y>::x. VC9 and Comeau go further and give an error on the freind
declaration itself, complaining that T is not a class (which it's not - it's
a template format parameter which can be bound to a class by instantiating
the template).

What compiler are you using? I'm guessing VC6 if this code actually
compiles.

-cd


.



Relevant Pages

  • Re: Circular Class Template Friendship
    ... > My issue is the syntax of declaring a friend class within ... > which the string is a fixed width that is specialized. ... it is still possible to have Name_Id_Table template ... the friend class declaration. ...
    (comp.lang.cpp)
  • Re: Template friendship to nested template class
    ... friend struct INNER; ... Probably you are missing an EXTRA template to declare ... I think it's because the name referred to in a 'friend' declaration is looked up to exist at the namespace scope. ...
    (microsoft.public.vc.language)
  • Re: Template friendship to nested template class
    ... template ... friend struct INNER; ... Both VC and gcc are happy with this code, but the intel compiler is ...
    (microsoft.public.vc.language)
  • Re: Templated friends
    ... > way for a compiler to figure out the template argument from the ... >> to do with friend. ... > of that template are friends), you need to begin your declaration ...
    (comp.lang.cpp)
  • Re: Class templates and friend function templates
    ... > expected the friend declaration to resolve to: ... > as a friend of C. ... The confusion stems from the expectation that function ... template instantiation is similar to class template ...
    (microsoft.public.vc.language)