You probably learned C++ a long time ago, then. You couldn't do
it in ARM C++; it was added by the standardization committee.
so I tried it, and you're right, it does work. The
output of the following program is
10 3.14159
class Foo
{
public:
template <class T> T Bar( T t ) { return t; };
};
int main()
{
Foo MyFoo;
std::cout << MyFoo.Bar<int>( 10 ) << " " << MyFoo.Bar<double>(
3.1415926 );
return 0;
At first I couldn't understand how it could work, wouldn't
there need to be an instance of the class for each type? Then
I realized the compiler only has to make one copy of the
method for each type. Interesting. Static variables (of type
T anyway) don't seem to be saved between calls however.
They should be. Could you give an example? (Remember that a
function template is NOT a function. Each instantiation of a
function template is a function. So each instantiation will
have its own local static variables.)
==========
I wish I had saved the code I was experimenting with. Because I thought the
same thing, and right now it is saving the instances of the static T
variables between calls, but it wasn't before. Unfortunately, I can't
reproduce it. I did what I thought was the exact same thing but now it's
retaining the value. Either some compiler bug, or most likely, PBKAC.