C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Template question

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
James Aguilar
Guest





PostPosted: Sat Dec 18, 2004 7:55 pm    Post subject: Template question Reply with quote



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


Back to top
Victor Bazarov
Guest





PostPosted: Sat Dec 18, 2004 8:21 pm    Post subject: Re: Template question Reply with quote



James Aguilar wrote:
Quote:
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?

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

Back to top
James Aguilar
Guest





PostPosted: Sat Dec 18, 2004 9:41 pm    Post subject: Re: Template question Reply with quote



"Victor Bazarov" <v.Abazarov (AT) comAcast (DOT) net> wrote

Quote:
James Aguilar wrote:
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?

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

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



Back to top
Ioannis Vranos
Guest





PostPosted: Sat Dec 18, 2004 11:34 pm    Post subject: Re: Template question Reply with quote

James Aguilar wrote:

Quote:
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.


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

Back to top
rixil@hotmail.com
Guest





PostPosted: Sun Dec 19, 2004 7:37 am    Post subject: Re: Template question Reply with quote

Ioannis Vranos wrote:
Quote:


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

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


Back to top
Ioannis Vranos
Guest





PostPosted: Sun Dec 19, 2004 3:37 pm    Post subject: Re: Template question Reply with quote

[email]rixil (AT) hotmail (DOT) com[/email] wrote:

Quote:
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 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.


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: "< }
};

template 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

Back to top
rixil@hotmail.com
Guest





PostPosted: Sun Dec 19, 2004 6:25 pm    Post subject: Re: Template question Reply with quote

Ioannis Vranos wrote:

Quote:
What you mean you did not define it.

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

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 void Foo<T>::bar() {
std::cout << "I am Foo::bar()...n";
}

Thus, what I defined was void Foo 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.

Quote:
And Foo<>::bar(); is the equivalent of Foo<int>::bar();

That is correct. Please re-read my post and see that that is exactly
what I said. To quote myself:

Quote:
As you can see in the example above, a call to
Foo<int>::bar() was made as Foo<>::bar().

What I was providing an example to help clarify your statement:

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

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().

Quote:
bar() is a different static function for each template instantiation
(type). Consider another example:

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

Regards,

Michael Loritsch


Back to top
Ioannis Vranos
Guest





PostPosted: Sun Dec 19, 2004 8:47 pm    Post subject: Re: Template question Reply with quote

[email]rixil (AT) hotmail (DOT) com[/email] wrote:

Quote:
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().


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

Back to top
rixil@hotmail.com
Guest





PostPosted: Sun Dec 19, 2004 9:28 pm    Post subject: Re: Template question Reply with quote

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

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.