Connecting Tech Pros Worldwide Forums | Help | Site Map

Template question

James Aguilar
Guest
 
Posts: n/a
#1: Jul 23 '05
Hey all,

Suppose I have a static function inside a template class that does not
depend on any of that class's parameters. Will I be able to access it
before I instantiate that template class (or even if I don't), or will it
not work until afterward?

James


Victor Bazarov
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Template question


James Aguilar wrote:[color=blue]
> Suppose I have a static function inside a template class that does not
> depend on any of that class's parameters. Will I be able to access it
> before I instantiate that template class (or even if I don't), or will it
> not work until afterward?[/color]

Any member of a class template is itself a template. For that member to
become a real thing, the template has to be instantiated. So, if you
make an attempt to use that function the compiler will be forced to
instatiate the template. So, the answer is "no", I guess.

Besides, how do you "access" that function _unless_ you actually specify
the template arguments?

V
James Aguilar
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Template question


"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:sj0xd.521730$wV.360170@attbi_s54...[color=blue]
> James Aguilar wrote:[color=green]
>> Suppose I have a static function inside a template class that does not
>> depend on any of that class's parameters. Will I be able to access it
>> before I instantiate that template class (or even if I don't), or will it
>> not work until afterward?[/color]
>
> Any member of a class template is itself a template. For that member to
> become a real thing, the template has to be instantiated. So, if you make
> an attempt to use that function the compiler will be forced to
> instatiate the template. So, the answer is "no", I guess.
>
> Besides, how do you "access" that function _unless_ you actually specify
> the template arguments?
>
> V[/color]

Ah, I hadn't really thought about that. The method does not actually depend
on the class, so I'm thinking about making it a non-member. I'm still
really used to Java constructs where _everything_ is part of a class, so I
sometimes think, "It would be better if this were in the class," when that
is really not the case.

Thanks,

James


Ioannis Vranos
Guest
 
Posts: n/a
#4: Jul 23 '05

re: Template question


James Aguilar wrote:
[color=blue]
> Ah, I hadn't really thought about that. The method does not actually depend
> on the class, so I'm thinking about making it a non-member. I'm still
> really used to Java constructs where _everything_ is part of a class, so I
> sometimes think, "It would be better if this were in the class," when that
> is really not the case.[/color]


Consider this:

class A
{
// ...
};


template <class T> SomeClass
{
// ...
};


A is a type.


SomeClass is *not* a type.

SomeClass<int> is a type.

SomeClass<float> is another type.



So if you define a static function (for some type), you have to use that
type to call it.


So SomeClass::somefunc(); is *not* legal since SomeClass is not a type,

while

SomeClass<int>::somefunc(); is legal since SomeClass<int> is a type.




--
Ioannis Vranos

http://www23.brinkster.com/noicys
rixil@hotmail.com
Guest
 
Posts: n/a
#5: Jul 23 '05

re: Template question


Ioannis Vranos wrote:[color=blue]
>
>
> So if you define a static function (for some type), you have to use[/color]
that[color=blue]
> type to call it.[/color]

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.

Therefore, one doesn't really define static functions when writing a
template class, merely, one defines the template so that the compiler
can generate static functions for us.

Furthermore, if your template class has default parameters, it is not
required of you to explicitly provide them in order to make a call to a
static function. As you can see in the example above, a call to
Foo<int>::bar() was made as Foo<>::bar().

Thus, while one must at least implicitly use a type to make a call to a
static function that is created as a result of a template
instantiation, explicit use of a type is not required, only the braces
<>.

Regards,

Michael Loritsch

Ioannis Vranos
Guest
 
Posts: n/a
#6: Jul 23 '05

re: Template question


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
rixil@hotmail.com
Guest
 
Posts: n/a
#7: Jul 23 '05

re: Template question


Ioannis Vranos wrote:
[color=blue]
> What you mean you did not define it.
>
> static void bar() {
> std::cout << "I am Foo::bar()...\n";
> }[/color]

I guess you must not be looking at the context in which bar() is
written. If one writes:

static void bar() {
std::cout << "I am Foo::bar()...\n";
}

within the context of the class template definition above, it is
equivalent to writing:

template <class T>
void Foo<T>::bar() {
std::cout << "I am Foo::bar()...\n";
}

Thus, what I defined was void Foo<T>::bar(), which is not a function,
but a template for creating functions.

The key distinction is that when you define a template for a function,
the compiler creates the function from your template. Therefore, one
never defines functions generated by the instantiation of a template,
one merely defines the template.
[color=blue]
> And Foo<>::bar(); is the equivalent of Foo<int>::bar();[/color]

That is correct. Please re-read my post and see that that is exactly
what I said. To quote myself:
[color=blue]
> As you can see in the example above, a call to
> Foo<int>::bar() was made as Foo<>::bar().[/color]

What I was providing an example to help clarify your statement:
[color=blue]
> So if you define a static function (for some type), you have to use[/color]
that[color=blue]
> type to call it.[/color]

I was showing an example where you don't have to explicitly use the
type int to make a call to Foo<int>bar(). In other words, Foo<>::bar()
was an example where you are not explicitly 'using the type int' to
call Foo<int>bar().
[color=blue]
> bar() is a different static function for each template instantiation
> (type). Consider another example:[/color]

Correct, this was never under question, and your example does not
conflict with anything I said.

Regards,

Michael Loritsch

Ioannis Vranos
Guest
 
Posts: n/a
#8: Jul 23 '05

re: Template question


rixil@hotmail.com wrote:
[color=blue]
> I was showing an example where you don't have to explicitly use the
> type int to make a call to Foo<int>bar(). In other words, Foo<>::bar()
> was an example where you are not explicitly 'using the type int' to
> call Foo<int>bar().[/color]


Yes, however Foo<> instance is using type int, so in all cases we use a
type even if we do not type it. :-)




--
Ioannis Vranos

http://www23.brinkster.com/noicys
rixil@hotmail.com
Guest
 
Posts: n/a
#9: Jul 23 '05

re: Template question


That is correct. That is why I said that we don't explicitly use the
type (i.e. type it), but rather use it implicitly.
Regards,

Michael Loritsch

Closed Thread


Similar C / C++ bytes