rixil@hotmail.com wrote:
[color=blue]
> While this statement is 100% true for non-template types, I feel it
> requires clarification when applied to classes that are created by the
> compiler as a result of a template instantiation.
>
> Consider the following valid code sample:
>
> #include <iostream>
>
> template <class T = int>
> class Foo {
> public:
> static void bar() {
> std::cout << "I am Foo::bar()...\n";
> }
> };
>
> int main() {
> Foo<>::bar();
> return 0;
> }
>
> The first point I would like to make is that I never actually defined
> the function Foo<int>::bar(), rather the compiler defined it for me.[/color]
What you mean you did not define it.
static void bar() {
std::cout << "I am Foo::bar()...\n";
}
The above part is the definition.
And Foo<>::bar(); is the equivalent of Foo<int>::bar();
bar() is a different static function for each template instantiation
(type). Consider another example:
#include <iostream>
template <class T = int>
class Foo
{
static int number;
public:
// Equivalent to Foo<T>
Foo() { ++number; }
static void bar()
{
std::cout << "Objects created so far: "<<number<<std::endl;
}
};
template<class T>
int Foo<T>::number=0;
int main()
{
Foo<> a, b;
Foo<>::bar();
Foo<float> c;
Foo<float>::bar();
}
C:\c>temp
Objects created so far: 2
Objects created so far: 1
C:\c>
Of course the compiler is free to optimise things out.
So what you have (you can think of) is a different static member
function for each template instance, since each instance is a different
type.
However the compiler can optimise it to one function.
--
Ioannis Vranos
http://www23.brinkster.com/noicys