How to grant friendship to a template class?

How to grant friendship to a template class?

Post by wm.. » Thu, 20 Jul 2000 04:00:00



Suppose we have one non-template and another
template class.

// Non-template class A
class A {
//  ???????
//   How to grant friendship to class B?
//

private:
       int i;   // datum that class B needs access to

Quote:};

// template class B
template <class T>
class B
{
public:
    A foo(const A & a )
    {
        // needs access to A::i
        // ....
    }

Quote:};

What is the syntax to grant friendship from inside
A to B?

I have tried:

    friend template<class T> class B<T>;
and
    friend class B<T>;

but neither compiled on Solaris SC5.0 or VC6.0.

What is the right syntax?

Thanks,
Wensheng

Sent via Deja.com http://www.deja.com/
Before you buy.

 
 
 

How to grant friendship to a template class?

Post by Rocky Raccoo » Thu, 20 Jul 2000 04:00:00



> Suppose we have one non-template and another
> template class.

> // Non-template class A
> class A {
> //  ???????
> //   How to grant friendship to class B?

        template<class T>
        friend class B;

But some compilers don't accept it.
GCC accepts it.

 
 
 

How to grant friendship to a template class?

Post by Ivan A. Teblyashki » Fri, 21 Jul 2000 04:00:00


 I guess that you can grant friendship only to particular classe, not to
class template. So, you should either do "friend class B<int>" or convert
your class A to template A<T> which grants friendship to B<T>. And the third
way is so trivial but the most helpfull inthis case is to create public
method that returns either variable or reference to it.

 Jone


>Suppose we have one non-template and another
>template class.

>// Non-template class A
>class A {
>//  ???????
>//   How to grant friendship to class B?
>//

>private:
>       int i;   // datum that class B needs access to

>};

>// template class B
>template <class T>
>class B
>{
>public:
>    A foo(const A & a )
>    {
>        // needs access to A::i
>        // ....
>    }
>};

>What is the syntax to grant friendship from inside
>A to B?

>I have tried:

>    friend template<class T> class B<T>;
>and
>    friend class B<T>;

>but neither compiled on Solaris SC5.0 or VC6.0.

>What is the right syntax?

>Thanks,
>Wensheng

>Sent via Deja.com http://www.deja.com/
>Before you buy.

 
 
 

How to grant friendship to a template class?

Post by Rocky Raccoo » Fri, 21 Jul 2000 04:00:00



>  I guess that you can grant friendship only to particular classe, not to
> class template.

Not true.
Check my post in the same thread.

> >Suppose we have one non-template and another
> >template class.

> >// Non-template class A
> >class A {
> >//  ???????
> >//   How to grant friendship to class B?
> >//

> >private:
> >       int i;   // datum that class B needs access to

> >};

> >// template class B
> >template <class T>
> >class B
> >{
> >public:
> >    A foo(const A & a )
> >    {
> >        // needs access to A::i
> >        // ....
> >    }
> >};

> >What is the syntax to grant friendship from inside
> >A to B?

> >I have tried:

> >    friend template<class T> class B<T>;
> >and
> >    friend class B<T>;

> >but neither compiled on Solaris SC5.0 or VC6.0.

> >What is the right syntax?

> >Thanks,
> >Wensheng

> >Sent via Deja.com http://www.deja.com/
> >Before you buy.

 
 
 

1. Granting friendship to a template parameter

How do I grant friendship to a class specified in a template parameter?

template <class T> class C {
  friend class T;         // <<< error

Comeau C/C++ Online complains that "template parameter T may not be used in
an elaborated type specifier".

I see that 7.1.5.3(2) specifically prohibits my declaration. Does anyone
know if there is a different syntax or a workaround that I can use?

MSVC allows the following (non-standard) code:

template <class T> class C {
    int i;
    friend T;  // note absence of 'class' keyword

struct T1 {
    T1() { C<T1> c; c.i; }  // access private member in C<T1>


      [ about comp.lang.c++.moderated. First time posters: do this! ]

2. Lowest Priced Hardware - Win A DVD

3. granting friendship to a translation unit.

4. coupon collection problem

5. Granting friendship to a policy

6. HW Engineering Employment

7. friendship between template classes

8. Digital UNIX ver3.x Thread Problem

9. Friendship of partially-specialised class templates?

10. Friendship between template classes

11. Use of class friendship before definition of class.

12. MFC Template Classes From Template Classes (in VC++2.2)

13. passing template class to template class