Topic: Friend template of a template
Author: Kresimir Fresl <fresl@grad.hr>
Date: 1997/01/27 Raw View
Greg Comeau wrote:
> Kresimir Fresl <fresl@grad.hr> writes:
>>What is the correct syntax to declare a friend class template
>>of a class template, if both classes should have same template
>>parameter? There are three cases:
...
>>(3) class A has other parameters:
>>
>> template <class T1, class T2> class A;
>> template <class T1> class B {
>> // A<T1, any_T2> should be friend of B<T1>
>> };
> How does your real code want to use this??
There's no real (working) code yet. Question occured while
I was musing about a design of a class `numarray', based
on David Vandevoorde's `valarray<T, M>'
(ftp://ftp.cs.rpi.edu/pub/vandevod/Valarray/).
Daveed's `valarray' has two template parameters: `T' is `element
type' and `M' is storage model:
template < class T, class M = c_array<T> >
class valarray {
// ...
private:
M m_;
};
And, for example, storage model `c_array<T>' is something like:
template <class T> class c_array {
// ...
private:
T* a_;
size_t sz;
};
(Other storage models are for slices and `static expression trees'.)
Users should be concerned only with `valarray's, and not with
storage models -- they should directly instantiate only `valarray's
with `c_array' storage model; therefore it is default parameter.
Moreover, users should not be allowed to instantiate objects of any
storage model (AFAICS, there can be some serious problems with slices
and `const-correctness'); only `valarray' should instantiate them.
Therefore, constructors (and some other member functions) of storage
model classes should be private, and `valarray' should be their friend.
[I am not sure that my `design' is completely correct -- this is
a theme for another discussion, probably not here, but in
c.l.c++.moderated. But anyway, why do you think that similar
situation cannot occur in `real code'?]
>>(Should class A be declared before class B?)
> If you mean what I think you do, yes.
Here's one example:
#include <iostream.h>
template <class T> class A;
template <class T> class B {
friend class A<T>;
T t;
public:
B (T t1) { t = t1; }
};
template <class T> class A {
public:
A (const B<T>& b) { cout << b.t << endl; }
};
main () {
B<double> b1 (5.3);
B<int> b2 (3);
B<char> b3 ('c');
A<double> a1 (b1);
A<int> a2 (b2);
A<char> a3 (b3);
}
With g++ (2.7.2) and Watcom C++ (10.6) this code can be compiled
without `forward declaration' of class B, while Comeau C++ 4.0
requires it.
K. Fresl
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: comeau@panix.com (Greg Comeau)
Date: 1997/01/24 Raw View
In article <fjh-970116-030446@cs.mu.oz.au> Kresimir Fresl
<fresl@grad.hr> writes:
>What is the correct syntax to declare a friend class template
>of a class template, if both classes should have same template
>parameter? There are three cases:
>
>(1) this parameter is the only parameter of both classes:
>
> template <class T1> class A;
> template <class T1> class B {
> // A<T1> should be friend of B<T1>
> // (eg. A<int> of B<int>, but not of B<double>)
friend class A<T1>;
> };
>
>(2) class B has other parameters:
>
> template <class T1> class A;
> template <class T1, class T2> class B {
> // A<T1> should be friend of B<T1, any_T2>
friend class A<T1>;
> };
>
>(3) class A has other parameters:
>
> template <class T1, class T2> class A;
> template <class T1> class B {
> // A<T1, any_T2> should be friend of B<T1>
> };
How does your real code want to use this??
>(Should class A be declared before class B?)
If you mean what I think you do, yes.
- Greg
--
Comeau Computing, 91-34 120th Street, Richmond Hill, NY, 11418-3214
Producers of Comeau C++ 4.0 front-end pre-release
****WEB: http://www.comeaucomputing.com / Voice:718-945-0009 /Fax:718-441-2310
Here:comeau@comeaucomputing.com / BIX:comeau or comeau@bix.com /CIS:72331,3421
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Kresimir Fresl <fresl@grad.hr>
Date: 1997/01/15 Raw View
What is the correct syntax to declare a friend class template
of a class template, if both classes should have same template
parameter? There are three cases:
(1) this parameter is the only parameter of both classes:
template <class T1> class A;
template <class T1> class B {
// A<T1> should be friend of B<T1>
// (eg. A<int> of B<int>, but not of B<double>)
};
(2) class B has other parameters:
template <class T1> class A;
template <class T1, class T2> class B {
// A<T1> should be friend of B<T1, any_T2>
};
(3) class A has other parameters:
template <class T1, class T2> class A;
template <class T1> class B {
// A<T1, any_T2> should be friend of B<T1>
};
(Should class A be declared before class B?)
Kresimir Fresl
---------------------------------------------------------------
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]