New GCC 4.0 template rules?

Oldtimer
Posts: 832
Joined: 2002.09
Post: #1
I have a template class setup that compiled and worked fine in GCC 2.7 (?) that doesn't compile in 4.0. The release docs state that the template rules have changed to conform better to the C++ specification, and I suppose that the following is due to that new and fancy two-level compilation. Source:

Code:
template <class T>
class Base
{
    public:
    Base<T> (T *_in)
    {
        delete _in;
    }
};

template <class T>
class Child: public Base <T>
{
    public:
    template <T>
    Child<T> (void):
        Base<T> (this)
    {
    }
};

class GrandChild: public Child<GrandChild>
{
    public:
        GrandChild (void):
            Child <GrandChild>()
        {
        }
};
Now, I'm not instantiating this anywhere, so if it is due to the two-level compilation, then it has to do with it being parsed when the template is specified, not instantiated.

The error message is:
/Users/fenris/temptest/main.cpp: In constructor `GrandChild::GrandChild()':
/Users/fenris/temptest/main.cpp:28: error: no matching function for call to 'Child<GrandChild>::Child()'
/Users/fenris/temptest/main.cpp:15: note: candidates are: Child<GrandChild>::Child(const Child<GrandChild>&)

Keith, help me! ;-)
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
The code is simply nonsensical. It doesn't compile with any C++ compiler I can find. That said, you've been overzealous with the templating. This works better, though it's still nonsensical:

Code:
template <class T>
class Base
{
        public:
        Base<T> (T *_in)
        {
                delete _in;
        }
};

template <class T>
class Child: public Base <T>
{
        public:
        Child():
                Base<T> (this)
        {
        }
};

class GrandChild: public Child<GrandChild>
{
        public:
                GrandChild():
                        Child <GrandChild>()
                {
                }
};
Quote this message in a reply
Oldtimer
Posts: 832
Joined: 2002.09
Post: #3
Thanks. Yes, of course it's nonsensical, it's an idealized version of the real problem. Smile But the top snippet did compile on whatever GCC version shipped with Jaguar...
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #4
I hope it didn't. Passing a Child<T>* (a subclass of Base<T>, which has no parent class) to a function which wants a T* should not work on any compiler. It's just plain illegal, and should be obvious to even the most naïve of compilers.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Xcode template for SDL felium 6 4,239 Aug 18, 2005 05:29 PM
Last Post: unknown