partially specialized friend

E

er

hi,

here's my problem:

template<class T,unsigned int I> class A;

template<unsigned int I>
class B{

// how 2 make A<T,I> a friend, with T as a template parameter?

};
 
J

jason.cipriani

hi,

here's my problem:

template<class T,unsigned int I> class A;

template<unsigned int I>
class B{

// how 2 make A<T,I> a friend, with T as a template parameter?

};

You will either have to specify the type T in the friend declaration:

template<class T,unsigned int I> class A;

template<unsigned int I>
class B {
friend class A<int,I>; // T is int
};

Or you will have to provide another template parameter to B to use for
T:

template<class T,unsigned int I> class A;

template<class T,unsigned int I>
class B {
friend class A<T,I>;
}

AFAIK there is no way to say "declare all A<T,I> as friends of B<I>
for any T". C++03 [14.5.3/9] reads:

Friend declarations shall not declare partial specializations.
[Example:
template<class T> class A { };
class X {
template<class T> friend class A<T*>; // error
};
--end example]

As a sanity check I tested the following:

template<class T,unsigned int I> A;

template<unsigned int I>
class B {
template<class T> friend class A<T,I>;
};

And it fails with the expected error:

"ComeauTest.c", line 11: error: a friend declaration may not declare a
partial
specialization
template <class T> friend class A<T,I>;

So I do not think it is possible.

Jason
 
E

er

here's my problem:
template<class T,unsigned int I> class A;
template<unsigned int I>
class B{
// how 2 make A<T,I> a friend, with T as a template parameter?

You will either have to specify the type T in the friend declaration:

template<class T,unsigned int I> class A;

template<unsigned int I>
class B {
friend class A<int,I>; // T is int

};

Or you will have to provide another template parameter to B to use for
T:

template<class T,unsigned int I> class A;

template<class T,unsigned int I>
class B {
friend class A<T,I>;

}

AFAIK there is no way to say "declare all A<T,I> as friends of B<I>
for any T". C++03 [14.5.3/9] reads:

Friend declarations shall not declare partial specializations.
[Example:
template<class T> class A { };
class X {
template<class T> friend class A<T*>; // error};

--end example]

As a sanity check I tested the following:

template<class T,unsigned int I> A;

template<unsigned int I>
class B {
template<class T> friend class A<T,I>;

};

And it fails with the expected error:

"ComeauTest.c", line 11: error: a friend declaration may not declare a
partial
specialization
template <class T> friend class A<T,I>;

So I do not think it is possible.

Jason

Thanks much. Adding a template parameter would B would be really
inconvenient in my case, as I'd have to do so for its clients as well.
I take it i'll just have to do without friend.
 
E

er

You will either have to specify the type T in the friend declaration:
template<class T,unsigned int I> class A;
template<unsigned int I>
class B {
friend class A<int,I>; // T is int

Or you will have to provide another template parameter to B to use for
T:
template<class T,unsigned int I> class A;
template<class T,unsigned int I>
class B {
friend class A<T,I>;

AFAIK there is no way to say "declare all A<T,I> as friends of B<I>
for any T". C++03 [14.5.3/9] reads:
Friend declarations shall not declare partial specializations.
[Example:
template<class T> class A { };
class X {
template<class T> friend class A<T*>; // error};
--end example]
As a sanity check I tested the following:
template<class T,unsigned int I> A;
template<unsigned int I>
class B {
template<class T> friend class A<T,I>;

And it fails with the expected error:
"ComeauTest.c", line 11: error: a friend declaration may not declare a
partial
specialization
template <class T> friend class A<T,I>;
So I do not think it is possible.

Thanks much. Adding a template parameter would B would be really
inconvenient in my case, as I'd have to do so for its clients as well.
I take it i'll just have to do without friend.

.... or dont' specialize: template<class,unsigned int> friend class A;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top