By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,890 Members | 1,226 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,890 IT Pros & Developers. It's quick & easy.

Is code generated in this template situation?

P: n/a

Hello all,

Consider this function template definition:

template<typename T>
void foo(T) {}

If foo is never called, this template will never be instantiated.

Now consider this explicit instantiation of foo:

template
void foo<int>(int);

Code will be generated for this explicit instantiation whether it is ever
called or not.

Now, let's assume the explicit instantiation above never occurred and
consider instead an explicit specialization:

template<>
void foo<int>(int) {}

My question is: Will code be generated for this explicit specialization if
it is never called?

Thanks,
Dave
Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"Dave" <be***********@yahoo.com> wrote...
Consider this function template definition:

template<typename T>
void foo(T) {}

If foo is never called, this template will never be instantiated.

Now consider this explicit instantiation of foo:

template
void foo<int>(int);

Code will be generated for this explicit instantiation whether it is ever
called or not.
Says who? And what do you mean by "code will be generated"? I can
only see the reverse in 14.7.3/6. If it's not defined, no implicit
instantiation is provided.
Now, let's assume the explicit instantiation above never occurred and
consider instead an explicit specialization:

template<>
void foo<int>(int) {}

My question is: Will code be generated for this explicit specialization if
it is never called?


What do you mean by "code will be generated"? An implicit specialisation
which is also a definition, the code exists (in your case the body is
empty). How can it not be "generated"? It's not a template, there is
no other decision to make.

Victor
Jul 22 '05 #2

P: n/a
Dave wrote:
....
template<>
void foo<int>(int) {}

My question is: Will code be generated for this explicit specialization if
it is never called?


gcc does.

Jul 22 '05 #3

P: n/a

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:F7oBb.484357$Tr4.1329921@attbi_s03...
"Dave" <be***********@yahoo.com> wrote...
Consider this function template definition:

template<typename T>
void foo(T) {}

If foo is never called, this template will never be instantiated.

Now consider this explicit instantiation of foo:

template
void foo<int>(int);

Code will be generated for this explicit instantiation whether it is ever called or not.
Says who? And what do you mean by "code will be generated"? I can
only see the reverse in 14.7.3/6. If it's not defined, no implicit
instantiation is provided.

That's what an explicit instantiation *does* - it explicitly directs the
compiler to substitute template arguments for template parameters and go
compile the result of this substitution into binary executable code (which
is what I meant by code being generated). Now, it may be that if the
explicitly instantiated function is never called, the linker will exclude
the executable code for that function from the final resulting executable,
but that does not change the fact that the executable code was generated.
Also, 14.7.3/6 deals with explicit specializations, not explicit
instantiations (they are two different things), so it is not relevant to
this portion of my post.
Now, let's assume the explicit instantiation above never occurred and
consider instead an explicit specialization:

template<>
void foo<int>(int) {}

My question is: Will code be generated for this explicit specialization if it is never called?


What do you mean by "code will be generated"? An implicit specialisation
which is also a definition, the code exists (in your case the body is
empty). How can it not be "generated"? It's not a template, there is
no other decision to make.

So if I read this correctly, you're contention is that, yes, providing a
definition for a function template specialization always causes executable
code to be generated for that specialization, even if that specialization is
never called. But now take the case of providing a definition for an
explicit full specialization of a class. Code is not generated for the
class' member functions which aren't called. I wasn't sure that the case
should necessarily be any different for a regular function, hence my
question...
Victor

Jul 22 '05 #4

P: n/a
"Dave" <be***********@yahoo.com> wrote...
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:F7oBb.484357$Tr4.1329921@attbi_s03...
"Dave" <be***********@yahoo.com> wrote...
Consider this function template definition:

template<typename T>
void foo(T) {}

If foo is never called, this template will never be instantiated.

Now consider this explicit instantiation of foo:

template
void foo<int>(int);

Code will be generated for this explicit instantiation whether it is ever called or not.
Says who? And what do you mean by "code will be generated"? I can
only see the reverse in 14.7.3/6. If it's not defined, no implicit
instantiation is provided.

That's what an explicit instantiation *does* - it explicitly directs the
compiler to substitute template arguments for template parameters and go
compile the result of this substitution into binary executable code (which
is what I meant by code being generated). Now, it may be that if the
explicitly instantiated function is never called, the linker will exclude
the executable code for that function from the final resulting executable,
but that does not change the fact that the executable code was generated.


I see no proof of this in the Standard (do you?). Exclusion of anything
by a [or the] linker is not mandated in the language specialisation. If
you're asking whether some compiler do that or not (like in Gianni's
answer), I have no idea (nor do I have any real interest in that, to be
honest with you).
Also, 14.7.3/6 deals with explicit specializations, not explicit
instantiations (they are two different things), so it is not relevant to
this portion of my post.
Yes, you're right. I often confuse the two.
Now, let's assume the explicit instantiation above never occurred and
consider instead an explicit specialization:

template<>
void foo<int>(int) {}

My question is: Will code be generated for this explicit
specialization if it is never called?
What do you mean by "code will be generated"? An implicit specialisation which is also a definition, the code exists (in your case the body is
empty). How can it not be "generated"? It's not a template, there is
no other decision to make.

So if I read this correctly, you're contention is that, yes, providing a
definition for a function template specialization always causes executable
code to be generated for that specialization, even if that specialization

is never called. But now take the case of providing a definition for an
explicit full specialization of a class. Code is not generated for the
class' member functions which aren't called.
Meaning, _no_ implicit instantiation takes place for members of
the specialised class template, right? Yes, the Standard says that.

I guess your "code will be generated" needs to be replaced with "no
[implicit] instantiation will take place", and then perhaps we will
understand each other...
I wasn't sure that the case
should necessarily be any different for a regular function, hence my
question...


Victor
Jul 22 '05 #5

P: n/a

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:9hqBb.5526$8y1.27333@attbi_s52...
"Dave" <be***********@yahoo.com> wrote...
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:F7oBb.484357$Tr4.1329921@attbi_s03...
"Dave" <be***********@yahoo.com> wrote...
> Consider this function template definition:
>
> template<typename T>
> void foo(T) {}
>
> If foo is never called, this template will never be instantiated.
>
> Now consider this explicit instantiation of foo:
>
> template
> void foo<int>(int);
>
> Code will be generated for this explicit instantiation whether it is ever
> called or not.

Says who? And what do you mean by "code will be generated"? I can
only see the reverse in 14.7.3/6. If it's not defined, no implicit
instantiation is provided.

That's what an explicit instantiation *does* - it explicitly directs the
compiler to substitute template arguments for template parameters and go
compile the result of this substitution into binary executable code (which
is what I meant by code being generated). Now, it may be that if the
explicitly instantiated function is never called, the linker will exclude the executable code for that function from the final resulting executable, but that does not change the fact that the executable code was generated.
I see no proof of this in the Standard (do you?). Exclusion of anything
by a [or the] linker is not mandated in the language specialisation. If
you're asking whether some compiler do that or not (like in Gianni's
answer), I have no idea (nor do I have any real interest in that, to be
honest with you).
Also, 14.7.3/6 deals with explicit specializations, not explicit
instantiations (they are two different things), so it is not relevant to
this portion of my post.
Yes, you're right. I often confuse the two.
Now, let's assume the explicit instantiation above never occurred and > consider instead an explicit specialization:
>
> template<>
> void foo<int>(int) {}
>
> My question is: Will code be generated for this explicit specialization
if
> it is never called?

What do you mean by "code will be generated"? An implicit

specialisation which is also a definition, the code exists (in your case the body is
empty). How can it not be "generated"? It's not a template, there is
no other decision to make.

So if I read this correctly, you're contention is that, yes, providing a
definition for a function template specialization always causes

executable code to be generated for that specialization, even if that

specialization is
never called. But now take the case of providing a definition for an
explicit full specialization of a class. Code is not generated for the
class' member functions which aren't called.
Meaning, _no_ implicit instantiation takes place for members of
the specialised class template, right? Yes, the Standard says that.

I guess your "code will be generated" needs to be replaced with "no
[implicit] instantiation will take place", and then perhaps we will
understand each other...

Let me reword now with the improved terminology that you suggested:

If a function template is explicitly specialized but never called, will "an
implicit instantiation" (strike "code generation" here) take place? Like
you, I am not interested in what any particular compiler does, only what the
Standard mandates. Nor am I concerned with whether or not the linker will
remove any executable code that's generated but not used. I desire only to
know if the Standard says anything about this one way or the other or if it
is left as implimentation-defined.

Hmmm, I wonder if my question is the same as: Is the physical location of
the definition of an explicit function template specialization a point of
instantiation? I don't know the terminology precisely enough to be sure if
that is the same question, but it may help lead to an answer...
I wasn't sure that the case
should necessarily be any different for a regular function, hence my
question...


Victor

Jul 22 '05 #6

P: n/a
"Dave" <be***********@yahoo.com> wrote...
[...]
Let me reword now with the improved terminology that you suggested:

If a function template is explicitly specialized but never called, will "an implicit instantiation" (strike "code generation" here) take place? Like
you, I am not interested in what any particular compiler does, only what the Standard mandates. Nor am I concerned with whether or not the linker will
remove any executable code that's generated but not used. I desire only to know if the Standard says anything about this one way or the other or if it is left as implimentation-defined.
The Standard says (in 14.7.3/6) "If the program does not provide
a definition for an explicit specialization and either the
specialization is used in a way that would cause an implicit
instantiation to take place or the member is a virtual member
function, the program is ill-formed, no diagnostic required. An
implicit instantiation is never generated for an explicit
specialization that is declared but not defined."

If that doesn't explain it, I don't know what will.

Hmmm, I wonder if my question is the same as: Is the physical location of
the definition of an explicit function template specialization a point of
instantiation?
Apparently not.
I don't know the terminology precisely enough to be sure if
that is the same question, but it may help lead to an answer...


Victor
Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.