473,657 Members | 2,667 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 1290
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
3168
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 { friend class TF; // error: using template type
6
3325
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 my particular application. One of the advantages is that the _compiler_ can force inner matrix dimensions used in multiplication to agree. A _complie-time_ error will be triggered if you write A * B and the number of coluns in A does not equal the...
10
1619
by: Asfand Yar Qazi | last post by:
Consider the following code: ========================================= #include <iostream> template<class ValueT, OwnerT> class Restricted_Public_Variable { public:
10
2199
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. (http://www.parashift.com/c++-faq-lite/containers-and-templates.html) And then, I pre-declared each template friend function above the definition of template class. But I still get template friends linker error. My compiler is gcc 3.3.3. Any hints? Thanks,
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 put_to_stream(std::ostream&) const;
5
6348
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; cout << val << endl;
2
5530
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 like this: template< class Type > class Singleton
12
1736
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
1905
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); }; template<typename T>
0
8829
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8734
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8508
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8608
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7341
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6172
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4323
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1962
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1627
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.