template parameter can't be friend - and we hack it to be - c++
This is a discussion on template parameter can't be friend - and we hack it to be - c++ ; gcc will complain if you try to compile this:
template <class T>
class TT
{
friend class T;
};
I googled and find people saying template parameters can't be friends.
So I do this:
// Add one more indirection
template ...
-
template parameter can't be friend - and we hack it to be
gcc will complain if you try to compile this:
template <class T>
class TT
{
friend class T;
};
I googled and find people saying template parameters can't be friends.
So I do this:
// Add one more indirection
template <class T>
struct FriendMaker
{
typedef T Type;
};
template <class T>
class Ty
{
friend class FriendMaker<T>::Type;
private:
int t;
};
class A
{
public:
void f(Ty<A>& ty)
{
std::cout << ty.t;
}
};
int main()
{
Ty<A> t;
A a;
a.f(t);
}
It compiles and works! Although I've not thought about how this can be
useful, i think someone might need it some day.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
-
Re: template parameter can't be friend - and we hack it to be
On Wed, 20 Jun 2007 09:33:14 CST, Ralph Zhang wrote:
>gcc will complain if you try to compile this:
>
>template <class T>
>class TT
>{
> friend class T;
>};
>
>I googled and find people saying template parameters can't be friends.
In C++03, yes.
>So I do this:
>
>// Add one more indirection
>template <class T>
>struct FriendMaker
>{
> typedef T Type;
>};
[...]
>It compiles and works!
Then, please, report the bug to the g++ guys :-)
--
\|||/ Gennaro Prota - For hire
(o o) https://sourceforge.net/projects/breeze/
--ooO-(_)-Ooo----- (to mail: name . surname / yaho ! com)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
-
Re: template parameter can't be friend - and we hack it to be
Ralph Zhang wrote:
> gcc will complain if you try to compile this:
>
> template <class T>
> class TT
> {
> friend class T;
> };
>
> I googled and find people saying template parameters can't be friends.
Yes, it's expressly prohibited.
> So I do this:
>
> // Add one more indirection
> template <class T>
> struct FriendMaker
> {
> typedef T Type;
> };
>
> template <class T>
> class Ty
> {
> friend class FriendMaker<T>::Type;
Comeau does not compile it this way, saying "Type" cannot be used
in elaborate type specifier. But it compiles if I change it to
friend typename FriendMaker<T>::Type;
Can just be a fluke...
> private:
> int t;
> };
>
Missing:
#include <iostream>
> class A
> {
> public:
> void f(Ty<A>& ty)
> {
> std::cout << ty.t;
> }
> };
>
> int main()
> {
> Ty<A> t;
> A a;
> a.f(t);
> }
>
> It compiles and works! Although I've not thought about how this can be
> useful, i think someone might need it some day.
Curious. Thanks for sharing!
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
-
Re: template parameter can't be friend - and we hack it to be
Victor Bazarov wrote:
>> template <class T>
>> class Ty
>> {
>> friend class FriendMaker<T>::Type;
>
> Comeau does not compile it this way, saying "Type" cannot be used
> in elaborate type specifier. But it compiles if I change it to
>
> friend typename FriendMaker<T>::Type;
>
> Can just be a fluke...
If it is a recent version of Comeau you should disable C++0x extensions.
--
\|||/ Gennaro Prota - For hire
(o o) https://sourceforge.net/projects/breeze/
--ooO-(_)-Ooo----- (to mail: name . surname / yaho ! com)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
-
Re: template parameter can't be friend - and we hack it to be
Gennaro Prota wrote:
> Victor Bazarov wrote:
>
>>> template <class T>
>>> class Ty
>>> {
>>> friend class FriendMaker<T>::Type;
>>
>> Comeau does not compile it this way, saying "Type" cannot be used
>> in elaborate type specifier. But it compiles if I change it to
>>
>> friend typename FriendMaker<T>::Type;
>>
>> Can just be a fluke...
>
> If it is a recent version of Comeau you should disable C++0x
> extensions.
Ah... Right. So, without C++0x extensions enabled, it won't compile,
but *with them* you just specify
friend T;
and no need for any FriendMaker. Thanks!
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Similar Threads
-
By Application Development in forum c++
Replies: 5
Last Post: 12-14-2007, 03:42 AM
-
By Application Development in forum c++
Replies: 5
Last Post: 09-17-2007, 08:13 PM
-
By Application Development in forum c++
Replies: 10
Last Post: 07-31-2007, 08:49 PM
-
By Application Development in forum c++
Replies: 1
Last Post: 07-01-2007, 07:51 PM
-
By Application Development in forum c++
Replies: 3
Last Post: 03-22-2007, 09:20 AM