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?

matt
  • 4,042
  • 5
  • 32
  • 50
  • As a data point, this produces the same error in GCC 4.3 on a couple of platforms, and the online Comeau C++ compiler (well known for thorough and good error messages) flags this as "error: type name is not allowed" on the `int` in `AFunc`. Thus, I think it's safe to say that the reason it doesn't work on the iPhone is that it's invalid code -- though I'm not posting this as an answer, in hopes that someone will answer with *why* it's invalid. – Brooks Moses Mar 10 '10 at 07:29

1 Answers1

2

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

mukeshkumar
  • 2,698
  • 3
  • 19
  • 20
  • 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 Mar 10 '10 at 07:38
  • 1
    Oh, now I see! Suppose that you produce a new specialization of AClass (for example) that defines a non-templated version of AFunc. And now suppose that you call ATestFunc(). 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 Mar 10 '10 at 07:46
  • 1
    See also: http://stackoverflow.com/questions/1840253/c-template-member-function-of-template-class-called-from-template-function – Brooks Moses Mar 10 '10 at 07:51
  • 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 Mar 10 '10 at 08:26