Jump to content
  • Advertisement
Sign in to follow this  
bradbobak

template in template strangeness

This topic is 4338 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, can someone explain to me the following:
template <class T>
class list
{
  public:
    T t;
};
template <class T>
class item
{
  public:
    list<T> *ptr;
};
template <class T>
class inherit : public item<T>
{
  public:
    inherit()
    {
      ptr = 0;
    }
};

int main(int, char **)
{
  inherit<int> i;
}
Now this won't compile in gcc 4, but if I change the 'ptr = 0;' to 'this->ptr = 0;' it does compile.. Can someone explain this to me??

Share this post


Link to post
Share on other sites
Advertisement
I think this particular feature of the c++ standard that you have run into is called ”two phase name lookup”.
I am not really good at explaining things but surely someone else (or google) can give a good explanation of exactly what’s happening.

[Edited by - M2 on July 7, 2006 8:57:10 AM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
just a quick note. msvc++ 2005 compiles this snippet just fine. btw, i don't really see why it shouldn't...

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
just a quick note. msvc++ 2005 compiles this snippet just fine. btw, i don't really see why it shouldn't...


It shouldn't compile (and I'm a little surprised it does in VS2005) because you could then do something like this:

EDIT: Actually, maybe it should.....the default is to treat a dependant name as a variable, which is the usage in this case.


template<>
class item<int>
{
public:
typedef list<int>* ptr;
};




Lookup 'dependant names'.

Share this post


Link to post
Share on other sites
Quote:
template <class T>
class inherit : public item<T>
{
public:
inherit()
{
ptr = 0;
}
};


Suppose the compiler where to try to parse the above code before you created it and passed it a type T.

The standard says "if a variable isn't dependant on the types passed to the template, it should be assumed that it isn't related to the types passed to the template."

That use of ptr doesn't mention the type T -- so the compiler is supposed to assume it is unrelated to the type T.

There are no ptr's in the scope that can be used. Error.

In MSVC, they don't do that preliminary parsing. They only do variable-existence checks after you pass in the type T, in violation of the standard. This means that they catch fewer syntax mistakes at template definition time, delaying it until you instantiate the template -- but it also means they can see the parent's ptr variable.

The advantage of doing the checks earlier is that you can be told "this template doesn't make sense, no matter what T you pass it!" more often, and that can lead to less ugly error messages.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Question: Why have a pointer to a template class list?

Share this post


Link to post
Share on other sites
Well in reality its a bit more complicated than that.. I just simplified as much as I could while still getting the error.. thus the pointer..

Share this post


Link to post
Share on other sites
Yes. Here's an example demonstrating why it is a Good Thing (tm) that you need to qualify the name:


template <typename T>
class foo
{
int bar;
};

// Let's specialize foo
template <>
class foo<float>
{
// NO variable named bar here!
};

// global variable bar
int bar;

template <typename T>
class derived_foo : foo<T>
{
void set_bar() { bar = 42; }
};

int main()
{
derived_foo<int> d1;
derived_foo<float> d2;

d1.set_bar(); // d1 inherits bar from foo<int>, so the call sets the member as expected
d2.set_bar(); // Oops! There's no bar in d2, so the call sets the global bar instead!
// This is certainly unexpected and error-prone.
}


Share this post


Link to post
Share on other sites
Thank you very much Sharlin, that explains it perfectly.. You also just taught me how to specialize templates.. Ratings++

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!