"James Kanze" <ja*********@gmail.comwrote in message
news:11**********************@57g2000hsv.googlegro ups.com...
On Aug 9, 2:38 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Victor Bazarov" <v.Abaza...@comAcast.netwrote in message
news:f9**********@news.datemas.de...
cheesi...@gmail.com wrote:
1. Can I declare some of template functions into a class? a concrete
class, not the template class.
Yes, you should be able to.
I didn't believe it,
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
#include <iostream>
class Foo
{
public:
template <class TT 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.