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 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();
}
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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
by: Asfand Yar Qazi |
last post by:
Consider the following code:
=========================================
#include <iostream>
template<class ValueT, OwnerT>
class Restricted_Public_Variable
{
public:
|
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.
...
|
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...
| |
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;...
|
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...
|
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;
};
|
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);
};
...
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |