Template question

J

James Aguilar

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
 
V

Victor Bazarov

James said:
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
 
J

James Aguilar

Victor Bazarov said:
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
 
I

Ioannis Vranos

James said:
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.
 
R

rixil

Ioannis said:
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
 
I

Ioannis Vranos

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.


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.
 
R

rixil

Ioannis said:
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 <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.
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:
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:
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 said:
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
 
I

Ioannis Vranos

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. :)
 
R

rixil

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,188
Messages
2,571,002
Members
47,591
Latest member
WoodrowBut

Latest Threads

Top