Template/friend compilation problem

D

derek.google

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.
 
V

Victor Bazarov

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
 
J

John Ratliff

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
 
D

derek.google

Victor said:
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.
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,609
Members
45,253
Latest member
BlytheFant

Latest Threads

Top