473,503 Members | 10,178 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

template parameters as friends

Why isn't this legal code and is there any other way to achieve the
effect I want (i.e. make a template parameter the friend of a template
class).
template <class X> class I
{
friend class X;
private:
I() {}
};

class C
{
public:
I<C> f() { return I<C>(); }
};

int main()
{
C c;
c.f();
}

john
Nov 24 '05 #1
5 1284
John Harrison wrote:
Why isn't this legal code and is there any other way to achieve the
effect I want (i.e. make a template parameter the friend of a template
class).
template <class X> class I
{
friend class X;
private:
I() {}
};

class C
{
public:
I<C> f() { return I<C>(); }
};

int main()
{
C c;
c.f();
}


This does not do quite what I think you want it to do but it's what I've
used in some cases.

template <class X> class I;

template <class D>
class I_Accessor
{
protected:
I<D> f() { return I<D>(); }
};

template <class X> class I
{
friend class I_Accessor<X>;
private:
I() {}
};

class C
: I_Accessor<C>
{
public:
I<C> f() { return I_Accessor<C>::f(); }
};

int main()
{
C c;
c.f();
}
Nov 24 '05 #2
Ian
John Harrison wrote:
Why isn't this legal code and is there any other way to achieve the
effect I want (i.e. make a template parameter the friend of a template
class).
template <class X> class I
{
friend class X;
private:
I() {}
};

class C
{
public:
I<C> f() { return I<C>(); }
};

int main()
{
C c;
c.f();
}

It should be, shouldn't it? It compiles fine for me.

Could it depend on when the compiler parses I? If this is done when
instantiating C, X (type C) is known. If not, X could be anything, for
example

class D
{
public:
I<int> f() { return I<int>(); }
};

Will fail as an int can't be a friend.

Ian
Nov 24 '05 #3
Gianni Mariani wrote:
John Harrison wrote:
Why isn't this legal code and is there any other way to achieve the
effect I want (i.e. make a template parameter the friend of a template
class).
template <class X> class I
{
friend class X;
private:
I() {}
};

class C
{
public:
I<C> f() { return I<C>(); }
};

int main()
{
C c;
c.f();
}

This does not do quite what I think you want it to do but it's what I've
used in some cases.

template <class X> class I;

template <class D>
class I_Accessor
{
protected:
I<D> f() { return I<D>(); }
};

template <class X> class I
{
friend class I_Accessor<X>;
private:
I() {}
};

class C
: I_Accessor<C>
{
public:
I<C> f() { return I_Accessor<C>::f(); }
};

int main()
{
C c;
c.f();
}


That's clever, I can make that work, thanks.

john
Nov 24 '05 #4
Ian wrote:
John Harrison wrote:
Why isn't this legal code and is there any other way to achieve the
effect I want (i.e. make a template parameter the friend of a template
class).
template <class X> class I
{
friend class X;
private:
I() {}
};

class C
{
public:
I<C> f() { return I<C>(); }
};

int main()
{
C c;
c.f();
}
It should be, shouldn't it? It compiles fine for me.


Which compiler? I tried VC++ 7.1, which just ignored the friend
declaration, so I got an access error, and g++ 3.4.4 which complained
about the friend declaration.

Could it depend on when the compiler parses I? If this is done when
instantiating C, X (type C) is known. If not, X could be anything, for
example

class D
{
public:
I<int> f() { return I<int>(); }
};

Will fail as an int can't be a friend.

Ian


The compiler has to parse a template twice. Once when it is first read
and once when it is instantiated. But exactly what should be done when
is not something I'm very familar with.

But now I'm thinking my code is probably just a syntax error. X isn't a
class name, its a template parameter so 'friend class X' is just wrong.
I guess I should try looking at the C++ standard, but asking here is
much easier.

john

Nov 24 '05 #5
Ian
John Harrison wrote:
It should be, shouldn't it? It compiles fine for me.

Which compiler? I tried VC++ 7.1, which just ignored the friend
declaration, so I got an access error, and g++ 3.4.4 which complained
about the friend declaration.

Sun CC.


The compiler has to parse a template twice. Once when it is first read
and once when it is instantiated. But exactly what should be done when
is not something I'm very familar with.
It depends how much checking it does when it is first read. I know that
gcc does more than the Sun compiler does, I think it parses all member
functions, where as the Sun compiler only does this when they are
instantiated.
But now I'm thinking my code is probably just a syntax error. X isn't a
class name, its a template parameter so 'friend class X' is just wrong.
I guess I should try looking at the C++ standard, but asking here is
much easier.

This could be a good one for comp.std.c++.

Ian
Nov 24 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3163
by: Gianni Mariani | last post by:
Does anyone know the wisdom behind this ? I want to make a template whose parameter needs to be a friend. What's the right(TM) way to do this ? template <typename T, typename TF> class Boo...
6
3309
by: Ben Ingram | last post by:
Hi all, I am writing a template matrix class in which the template parameters are the number of rows and number of columns. There are a number of reasons why this is an appropriate tradeoff for...
10
1602
by: Asfand Yar Qazi | last post by:
Consider the following code: ========================================= #include <iostream> template<class ValueT, OwnerT> class Restricted_Public_Variable { public:
10
2185
by: william xuuu | last post by:
Actually, I also got linker errors with template functions and template classes. And I avoided both of them successfully, by pouring foo.cpp into foo.h, according to the C++ FAQ. ...
4
342
by: Gabriel | last post by:
Hi I've got a question about template specializations. consider a template class like e.g. template<typename T> class Settings_Value { // other shit private: virtual void...
5
6339
by: rf | last post by:
As I understand it, it is legal to have a template member function of a non-template class, as in the following example: class XYZ { public: template <class Tvoid fn(T* ptr) { T val = *ptr;...
2
5514
by: Dave Rudolf | last post by:
Hey all, So I have a template class whose only template parameter is a type that the class is to manage. It's actually an implementation of the common Singleton design pattern. The class looks...
12
1723
by: siddhu | last post by:
Hello, would you suggest to me, why gcc 3.3.3 can not compile this: template<class T> class Base { Base(){} friend T; };
4
1893
by: StephQ | last post by:
According to: http://www.parashift.com/c++-faq-lite/templates.html#faq-35.4 , if my understanding is correct, in template<typename T> class Foo { friend void func (const Foo<T>& foo); }; ...
0
7207
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7095
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7294
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7361
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7015
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7470
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5026
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
1
749
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
403
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.