c++iphonetemplates

iphone compiler inherited templated base classes with passed through type not being expanded in time (just look)


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?


Solution

  • 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