views:

39

answers:

1
template<typename T> struct AClass
{
public:
     template<typename T0>
     static void AFunc()
     {}
};

template<typename T>
void ATestFunc()
{
     AClass<T>::AFunc<int>();
}

this works on other platforms, but not on the iPhone I get an error " expected primary-expression before 'int' " on the line where I call the function.

it works fine if I was to do

AClass<int>::AFunc<int>();

and it works fine if we ditch the template parameter for the function as well:

template<typename T> struct AClass
{
public:

     static void AFunc()
     {}
};

template<typename T>
void ATestFunc()
{
     AClass<T>::AFunc();
}

Any Ideas as to why it doesn't work with the iPhone?

+2  A: 

try changing the line AClass<T>::AFunc<int>() to AClass<T>::template AFunc<int>();

hype
W. T. F. WHY???? :'(Thanks! this worked perfectly! (well, it compiles anyway) feel free to show your working, I have know idea why this is needed :)
matt
Oh, now I see! Suppose that you produce a new specialization of AClass<float> (for example) that defines a non-templated version of AFunc. And now suppose that you call ATestFunc<float>(). That would reference the specialized version of AClass, which will reference the non-template version of AFunc. Thus, at the point where you're writing ATestFunc, the compiler cannot know whether AFunc is actually templated or not, since it doesn't know whether you'll be referencing this AClass specialization or another one, and so you need to tell it explicitly that it's templated.
Brooks Moses
See also: http://stackoverflow.com/questions/1840253/c-template-member-function-of-template-class-called-from-template-function
Brooks Moses
So after all this I can easily see why VC++ and other compilers just completely ignore templated code until it is actually used and instantiated, and thus the compiler knows all the template parameters.the main downside is that sometimes syntax errors won't come up until some poor soul actually goes to try and use the function. but honestly, you should be testing your code anyway, and I say, if it avoids complicating an already extremely complicated topic such as templates, I'm all for it!
matt