On Aug 9, 2:38 am, "Jim Langston" <tazmas...@rocketmail.com> wrote:
> "Victor Bazarov" <v.Abaza...@comAcast.net> wrote in message
> news:f9df7j$76v$1@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 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.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34