Is code generated in this template situation?

D

Dave

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
 
V

Victor Bazarov

Dave said:
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
 
D

Dave

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

Gianni Mariani

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

Victor Bazarov

Dave said:
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.
specialization
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
 
D

Dave

Victor Bazarov said:
Dave said:
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.
specialization
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...
 
V

Victor Bazarov

Dave said:
[...]
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
 

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

Forum statistics

Threads
474,155
Messages
2,570,871
Members
47,401
Latest member
CliffGrime

Latest Threads

Top