473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Template/friend compilation problem

While porting an application to Linux I hit a strange compiler error
with GCC 3.3.2. Here is the most stripped down version of the code I
could write:

1 template <typename T> struct SmartPtr
2 {
3 SmartPtr(T*) {}
4 ~SmartPtr() { delete pm_impl; }
5 T* pm_impl;
6 };
7
8 class YYY;
9
10 template <class T>
11 class XXX
12 {
13 friend class SmartPtr< XXX<YYY> >;
14 ~XXX() {}
15 };
16
17 class YYY {};
18
19 int main()
20 {
21 SmartPtr< XXX<YYY> > p(new XXX<YYY>());
22 }

The compiler error is:

: In destructor `SmartPtr<T>::~SmartPtr() [with T = XXX<YYY>]':
:21: instantiated from here
:14: error: `XXX<T>::~XXX() [with T = YYY]' is private
:4: error: within this context

Of course GCC is right that ~XXX() is private, but the friend
declaration is there to take care of that. Other compilers (Comeau,
VC6, VC7) seem to have no problem with this code. Is this a GCC bug?
Can anyone suggest how to make this compile without making ~XXX()
public?

Thanks.

Oct 19 '05 #1
3 1614
de**********@grog.net wrote:
While porting an application to Linux I hit a strange compiler error
with GCC 3.3.2. Here is the most stripped down version of the code I
could write:
[...]

Of course GCC is right that ~XXX() is private, but the friend
declaration is there to take care of that. Other compilers (Comeau,
VC6, VC7) seem to have no problem with this code. Is this a GCC bug?
Nah... Gotta be the bug in all other compilers...
Can anyone suggest how to make this compile without making ~XXX()
public?


Have you tried gnu.g++.help newsgroup? Have you tried upgrading your
compiler? They have 4.0 out, you know.

V
Oct 20 '05 #2
de**********@grog.net wrote:
While porting an application to Linux I hit a strange compiler error
with GCC 3.3.2. Here is the most stripped down version of the code I
could write:

1 template <typename T> struct SmartPtr
2 {
3 SmartPtr(T*) {}
4 ~SmartPtr() { delete pm_impl; }
5 T* pm_impl;
6 };
7
8 class YYY;
9
10 template <class T>
11 class XXX
12 {
13 friend class SmartPtr< XXX<YYY> >;
14 ~XXX() {}
15 };
16
17 class YYY {};
18
19 int main()
20 {
21 SmartPtr< XXX<YYY> > p(new XXX<YYY>());
22 }

The compiler error is:

: In destructor `SmartPtr<T>::~SmartPtr() [with T = XXX<YYY>]':
:21: instantiated from here
:14: error: `XXX<T>::~XXX() [with T = YYY]' is private
:4: error: within this context

Of course GCC is right that ~XXX() is private, but the friend
declaration is there to take care of that. Other compilers (Comeau,
VC6, VC7) seem to have no problem with this code. Is this a GCC bug?
Can anyone suggest how to make this compile without making ~XXX()
public?

Thanks.


Upgrade your g++. It compiles in 3.4.2, btw, but not in 3.3.3.

g++ 3.3.x has a few problems... :-(

--John Ratliff
Oct 20 '05 #3
Victor Bazarov wrote:
Of course GCC is right that ~XXX() is private, but the friend
declaration is there to take care of that. Other compilers (Comeau,
VC6, VC7) seem to have no problem with this code. Is this a GCC bug?


Nah... Gotta be the bug in all other compilers...


Would that really be so strange? I have multiple examples of invalid
C++ that are rightfully rejected by GCC but are happily compiled by
VC6, VC7, Borland, and Sun Forte. Sometimes all other compilers do get
it wrong, or at least all other compilers that I have access to.
Can anyone suggest how to make this compile without making ~XXX()
public?


Have you tried gnu.g++.help newsgroup? Have you tried upgrading your
compiler? They have 4.0 out, you know.


Sadly in the real world we can't always upgrade to the latest compiler
just because of a bug. Unless you have a way to convince all my
third-party library vendors that they need to support GCC 4.0 tomorrow.
:-) I'm using the version I'm using precisely because it's supported by
vendors I depend on.

But thanks just the same. I'll just make the dtor public and that'll
be the end of it.

Oct 20 '05 #4

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

Similar topics

2
by: Alex Vinokur | last post by:
========================================= Windows 2000 CYGWIN_NT-5.0 1.3.22(0.78/3/2) GNU gcc version 3.2 20020927 (prerelease) ========================================= Here is some program...
3
by: Ike Naar | last post by:
Given the following C++ snippet: template < typename T > class A { // 1 public : // 2 class AA { } ; // 3 } ; ...
6
by: Adam Parkin | last post by:
Hello, all I'm having a problem with friend functions in a templatized Queue class I'm writing using linked lists. The problem is that I can't get the friend function to be able to access private...
5
by: truce | last post by:
I searched but couldn't find anything _quite_ like this. I have some code I inherited that needs some work. Basically I have a template that is instantiated in the c++ code and so only the...
3
by: Pierre Barbier de Reuille | last post by:
Hi, after reading the article " The Standard Librarian : Defining Iterators and Const Iterators" from Matt Austern:...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
11
by: Grizlyk | last post by:
New C++ rules are always trying to do "fast" compilation - to compile code, which must be compiled at point of instance only (not during declaration stage). The behaviour is intoducing many wrong...
0
by: abelahcene | last post by:
Hi, Is it a bug ?? I tried to compile a cpp program which ran correctly on previous release (sarge debian, gcc compiler 3 ), it gave errors, for the declaration (even if I omit the symbols <) ...
7
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
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...
0
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...
0
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...

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.