473,224 Members | 1,369 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,224 software developers and data experts.

Is code generated in this template situation?


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
6 1989
"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
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

"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
"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

"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: David Coppit | last post by:
Hi all, Most smart pointers remove the need to call delete. What I want is a pointer that turns delete into a no-op for "unauthorized" clients. The reason is that I'm generating code from...
1
by: Martin | last post by:
I have a couple of questions around code signing with MS technology: 1. Is there a way to transfer the generated strong name signing private key directly to a smartcard (or generate it on the...
6
by: RainBow | last post by:
Greetings!! I introduced the so-called "thin-template" pattern for controlling the code bloat caused due to template usage. However, one of the functions in the template happens to be virtual...
4
by: John Holmes | last post by:
I'm using data to rename some web controls on a form that uses a repeater contol and so it can have mulitple instances of the same control set. The controls get renamed (thanks to Steven Cheng's...
6
by: TPJ | last post by:
Help me please, because I really don't get it. I think it's some stupid mistake I make, but I just can't find it. I have been thinking about it for three days so far and I still haven't found any...
4
by: VMI | last post by:
In the next few weeks, we'll be discussing what standards will be used for our web development, and one of the suggestions was to use a code generator (in our case, the first version of LLBLGen)....
10
by: Ivan Vecerina | last post by:
Here's a relatively simple code snippet: #include <memory> class Base { public: Base(); virtual ~Base(); virtual void f(int a, char const* name);
1
by: Ian | last post by:
I've just discovered the msclr::lock class in the C++ Support Library online documentation. This seems like a much cleaner way to implement thread protection than using...
12
by: StephQ | last post by:
I face the following problem. I wrote a poor's man plotting function: computePlot. This function append some x-values belonging to step and the correseponding f( x ) (for a given const member...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.