Topic: Templates and friends
Author: gerhard.menzl@sea.ericsson.se
Date: 1999/01/18 Raw View
Siemel Naran wrote:
> >My understanding of 14.5.3 and the examples provided therein is that
> >
> > template<typename T> class X
> > {
> > template<typename T> friend class Y;
> > };
> >makes every specialization of Y a friend of the *corresponding*
> >specialization of X (i.e. Y<int> is a friend of X<int>, and so on).
>
> The parameter 'T' of the friend declaration hides the parameter
> 'T' of the class declaration. That is, the second 'T' hides the
> first. So the compiler should give you a warning. Other than
> that, the named argument 'T' in the friend declaration is not
> needed, and the above is equivalent an infinite number of friend
> declarations, as follows:
> template<typename T> class X
> {
> friend class Y<int>;
> friend class Y<double>;
> ...
> };
>
> In short, for every T1 and every T2, X<T1> is a friend to Y<T2>.
> If every T1 and T2 were instantiated, there would be
> infinity*infinity friend declarations. Your example is that
> Y<int> is a friend of X<int>, Y<double> is a friend of X<int>,
> Y<...> is a friend of X<int>. That is, it is equivalent to:
> template <typename> class Y;
> template<typename T> class X { template <typename T2> friend class
Y<T2>; };
Can you point me to the part of the standard where it says that a
template parameter in a friend declaration hides a template parameter
of the same name in the enclosing template class definition?
Gerhard Menzl
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: sbnaran@localhost.localdomain.COM (Siemel Naran)
Date: 1999/01/18 Raw View
On 18 Jan 99 10:07:54 GMT, gerhard.menzl@sea.ericsson.se
>Can you point me to the part of the standard where it says that a
>template parameter in a friend declaration hides a template parameter
>of the same name in the enclosing template class definition?
Sorry, I couldn't find a sentence in the section on templates that
actually says this. Possibly, they say it in the general section,
where they discuss the general concept of hiding (or at least I
think they probably discuss this concept somewhere).
But the general concept of hiding is the same anywhere, so I
figure it applies to templates too. Eg,
int main()
{
int i=3;
do
{
int i=7; // hides previous 'i'
cout << i; // prints "7"
} while (false);
}
struct Base
{
void f(int);
};
struct Derived : Base
{
void f(double);
};
int main()
{
Derived().f(3); // calls Derived::f(double)
}
--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Gerhard Menzl <gerhard.menzl@sea.ericsson.se>
Date: 1999/01/14 Raw View
My understanding of 14.5.3 and the examples provided therein is that
template<typename T> class X
{
template<typename T> friend class Y;
};
makes every specialization of Y a friend of the *corresponding*
specialization of X (i.e. Y<int> is a friend of X<int>, and so on).
However, the developer support of Microsoft, whose recent compiler
rejects the code altogether, thinks that it makes every
specialization of Y a friend of every specialization of X. They
claim that in order to achieve the former effect you would have to
declare:
template<typename T> class X
{
friend class Y;
};
Who is right?
Gerhard Menzl
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: sbnaran@localhost.localdomain (Siemel Naran)
Date: 1999/01/14 Raw View
On 14 Jan 99 17:12:40 GMT, Gerhard Menzl <gerhard.menzl@sea.ericsson.se> wrote:
[PART I]
>My understanding of 14.5.3 and the examples provided therein is that
>
> template<typename T> class X
> {
> template<typename T> friend class Y;
> };
>makes every specialization of Y a friend of the *corresponding*
>specialization of X (i.e. Y<int> is a friend of X<int>, and so on).
The parameter 'T' of the friend declaration hides the parameter
'T' of the class declaration. That is, the second 'T' hides the
first. So the compiler should give you a warning. Other than
that, the named argument 'T' in the friend declaration is not
needed, and the above is equivalent an infinite number of friend
declarations, as follows:
template<typename T> class X
{
friend class Y<int>;
friend class Y<double>;
...
};
In short, for every T1 and every T2, X<T1> is a friend to Y<T2>.
If every T1 and T2 were instantiated, there would be
infinity*infinity friend declarations. Your example is that
Y<int> is a friend of X<int>, Y<double> is a friend of X<int>,
Y<...> is a friend of X<int>. That is, it is equivalent to:
template <typename> class Y;
template<typename T> class X { template <typename T2> friend class Y<T2>; };
[PART II]
>However, the developer support of Microsoft, whose recent compiler
>rejects the code altogether, thinks that it makes every
>specialization of Y a friend of every specialization of X. They
>claim that in order to achieve the former effect you would have to
>declare:
>
> template<typename T> class X
> {
> friend class Y;
> };
No, this makes Y a friend of X<int>, Y a friend of X<int>. It is
the same as,
class Y;
template<typename T> class X { friend class Y; };
To get Y<int> a friend of X<int>, Y<double> a friend of X<double>,
etc, I think the only way is to forward declare class Y at namespace
scope. Therefore,
template <typename> class Y;
template<typename T> class X { friend class Y<T>; };
>Who is right?
In part I, I think Microsoft is right.
In part II, I think Microsoft is wrong.
--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]