Topic: friend typename


Author: "Earl Purple" <earlpurple@gmail.com>
Date: Thu, 8 Jun 2006 10:04:04 CST
Raw View
LuB wrote:

> Is the following snippet legal syntax?
>
>
> template<typename T>
> class A
> {
>     // friend class T;     <----- illegal per  [7.1.5.3/2] (thank you
> Kai-Uwe Bux)
>
>     friend typename T;  // compiles on msvc 2005, fails on g++ variants
> (cygwin 3.3.3 and redhat 3.2.3)
>
>     int intVal;
> };

friend typename T; is not standard but some compilers have been
extended to allow it.

The workaround can be to derive T from A<T> and have the members of
A<T> that you want T to access as protected. The downside of that, of
course, is that your T is exposed to the implementation details of
A<T>, it being a template. T might access A<T> in its implementation
only. (Probably does). But you can always "pImpl" this anyway so the T
that derives from A<T> is a pImpl class (not really important if it
doesn't encapsulate, as you don't really have to encapsulate
encapsulations).

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "LuB" <lutherbaker@yahoo.com>
Date: Fri, 26 May 2006 11:18:00 CST
Raw View
Is there a distinction in the std between

  friend class T;

  and

  friend typename T;


I _think_ they both fall under [7.1.5.3/2] but I often misunderstand
the standard. Since "typename" is used in many places to disambiguate
types from other template parameters, I am wondering if this is one of
those .. or simply a compiler error.

Is the following snippet legal syntax?


template<typename T>
class A
{
    // friend class T;     <----- illegal per  [7.1.5.3/2] (thank you
Kai-Uwe Bux)

    friend typename T;  // compiles on msvc 2005, fails on g++ variants
(cygwin 3.3.3 and redhat 3.2.3)

    int intVal;
};

class B
{
public:
    void init (A<B>& a)
    {
        a.intVal = 6;
    }
};

int main(int argc, char** argv)
{
    A<B> ab;
    B b;
    b.init(ab);
    return 0;
}

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]