473,583 Members | 2,858 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is code generated in this template situation?


Hello all,

Consider this function template definition:

template<typena me 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 2008
"Dave" <be***********@ yahoo.com> wrote...
Consider this function template definition:

template<typena me 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.********@com Acast.net> wrote in message
news:F7oBb.4843 57$Tr4.1329921@ attbi_s03...
"Dave" <be***********@ yahoo.com> wrote...
Consider this function template definition:

template<typena me 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.********@com Acast.net> wrote in message
news:F7oBb.4843 57$Tr4.1329921@ attbi_s03...
"Dave" <be***********@ yahoo.com> wrote...
Consider this function template definition:

template<typena me 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.********@com Acast.net> wrote in message
news:9hqBb.5526 $8y1.27333@attb i_s52...
"Dave" <be***********@ yahoo.com> wrote...
"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:F7oBb.4843 57$Tr4.1329921@ attbi_s03...
"Dave" <be***********@ yahoo.com> wrote...
> Consider this function template definition:
>
> template<typena me 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
1465
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 pre-existing legacy code that assumes the caller deletes the pointer. However, I want to reduce memory copies for the case where there are multiple...
1
3192
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 smart card), without the unsecure intermediate storage to the filesystem using sn -k and sn -i? 2. What is the format of the key files produced by sn...
6
3994
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 as well. To support thin-template, I need to make virtual function as inline. Now, I know that compiler would generate an out-of-line copy when it
4
2048
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 help) in a click event of the button that is pushed to enter data. Each time the button is clicked it sends the ID entered by the user to query the...
6
2336
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 solution. My code can be downloaded from here: http://www.tprimke.net/konto/PyObject-problem.tar.bz2. There are some scripts for GNU/Linux system...
4
1428
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). Personally, I don't like code generators. I inherited two web applications that use LLBLGen, and they are just impossible to debug. It generates so...
10
1731
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
6402
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 monitor::enter/try/finally/monitor exit. However, I cannot get around one of the warning messages and am requesting help in understanding what is causing the warning. All...
12
2409
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 function f that takes and return a double) values to a reference stringstream, let's call this ss. Then I usually use ss.str() to transfer the...
0
8314
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7922
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8185
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6571
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5366
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3811
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2317
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1416
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1147
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.