views:

36

answers:

2

Try this out:

    template <typename T>
    class Base
        {
        public:
            int someBaseMember;
        };

    template <typename T>
    class Test: public Base<T>
    {
    public:
        void testFunc()
        {
            someBaseMember = 0;
        }
    };

In vc++ and the psp compiler (and any other compiler I've encountered) the above will work fine, with the iphone compiler (for device, gcc 4.2 I think, with the -fpermissive flag set) I get an error saying 'someBaseMember is not defined' on the 'someBaseMember = 0;' line

The iphone compiler seems to be 'parsing' templated code a lot sooner than other compilers do, (from what I can tell, most others don't even syntax check them until you actually CALL the function, or instantiate an instance.)

From what I can tell its parsing it so soon that it hasn't even parsed the base class yet :S its like it doesn't exist.

Any Ideas?

+4  A: 

The error that you are getting is correct (the other compilers should not accept the code and are doing so erroneously); the variable someBaseMember depends on the template instantation of Base<T>, but this dependence has not been expressed in your usage, and hence the compiler is correct in attempting to resolve it independently of the template parameter.

You can resolve this problem by making this dependence explicit, thereby forcing the compiler to resolve the variable using the template instantation. You can use either of the following:

this->someBaseMember = 0;

OR

Base<T>::someBaseMember = 0;

Either of the above should result in the resolution mechanism you want.

EDIT
You might want to see the relevant section of the C++ FAQ Lite:
http://www.parashift.com/c++-faq-lite/templates.html#faq-35.19
http://www.parashift.com/c++-faq-lite/templates.html#faq-35.18

Michael Aaron Safyan
well that's stupid... but thanks guys!
matt
+2  A: 

someBaseMember is a name that doesn't seem to depend on the template parameters, so it's not a dependent name, as the standard calls it.

The C++ name lookup rules cause the compiler to not look in the templated base class for this name, since it's not a dependent name. To work around this, you can use this-> to make clear that someBaseMember is a member of the class (and so implicitly dependent on the template parameters):

this->someBaseMember = 0;

This is not specific to the iphone compiler, but defined like that in the language. See also this entry in the C++ FAQ Lite for more details.

sth