Solved

Error checking in templates

Posted on 2002-06-23
12
173 Views
Last Modified: 2010-04-01
Does error checking take place for each instantiation, even if the types generated are the same?
0
Comment
Question by:Kezia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
12 Comments
 
LVL 8

Expert Comment

by:fl0yd
ID: 7102664
Could you be a bit more precise? You are talking about c++-templates, right? What error checking exactly are you talking about?
0
 

Expert Comment

by:nke2000
ID: 7105071
If you code error handling, it gets called no matter what the type.  Error handling (checking) code is not essentially different from other code.  However, your catch handlers may be different depending on the type.
0
 
LVL 8

Expert Comment

by:fl0yd
ID: 7105146
@ nke2000:
This is not necessarily true. Think about assert(), for example, which is only compiled in DEBUG-mode.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Kezia
ID: 7106906
When a template is compiled, it is checked for syntax errors, that is without the template arguments. When you instantiate a template, the compiler checks for errors in the generated specialization, that is with the template arguments.

My question is does the compiler check for errors during each instantiation, even if the generated types are the same; meaning, if I pass parameters of the same kind, during creation of two instances of the template, does the compiler check for errors for each instance, since the generated types in both cases would be the same (same template parameters being passed)?
0
 
LVL 8

Expert Comment

by:fl0yd
ID: 7107460
This is absolutely implementation specific to each compiler. Unless you have the source code there is no way of telling whether a specific revision of a given compiler does or doesn't. A fast compiler will only compile the code once, a bad compiler might use a different approach.
I still don't understand the question, though. Is this in any way relevant to you as the developer?
0
 

Expert Comment

by:nke2000
ID: 7107553
Seems like it would.  It checks for errors when you use any built in type multiple times. I see no reason why a compiler would give a user defined type a free pass after the first use.
0
 

Author Comment

by:Kezia
ID: 7111108
fl0yd,
If this kind of error checking is implementation specific to the compiler, then it has no relevance to me as a developer.

nke2000,
In a way, you seem to be contradicting to fl0yd, you think the compiler will do the error checking each time, irrespective of whether the type is user-defined or built-in type.

I wouldn't know which of you is correct.

Thanks for your comments anyway.
Kezia.
0
 
LVL 8

Accepted Solution

by:
fl0yd earned 100 total points
ID: 7111829
Well, a computer doesn't lie -- so one of us needs to be wrong. I think, however, that this is due to a misunderstanding on either my part or nke2000's.
If I understood you correctly you are talking about having something like

std::vector<int> v1;
std::vector<int> v2;

and want to find out whether the std::vector<int>::vector()-function [constructor] is checked for errors again when v2 is declared, or whether it is known to the compiler that it has already been compiled [when v1 was declared] and therefore needs no further checking. If so, this is completely contained within the compiler. Any compiler can do as it pleases as long as it is in compliance with the standard. It doesn't make any difference to the developer though. In fact, the only time when you would need to know which approach to take is when you are writing a compiler yourself.
To clearify things, maybe you could tell us why you need to know or whether it's just an 'out-of-curiosity' thing.
0
 

Expert Comment

by:nke2000
ID: 7112371
fl0yd,
Thanks for taking time to clarify the issue.

I wonder about your statement, "...whether it is known to the compiler..and therefore needs no further checking." Could that condition obtain for any compiler?  How could the compiler satisfy the standards' strict requirements for type checking and optimize the second check away?  
0
 
LVL 8

Expert Comment

by:fl0yd
ID: 7113593
@ nke2000:
I'm not sure that Kezia is really talking about this. Only he would know whether I interpreted his question correctly or not.
For your other two questions: A compiler can keep a list of objects that it has encountered so far an use it as a lookup-table to check if a piece of code needs to be compiled or has already been so in the past. This type of 'optimization' has nothing to do with type-safety though. If you take the above example:

std::vector<int> v1;
std::vector<int> v2;

does it really make it any more safe to check the std::vector<int>-constructor the second time around? If it was correct the first time it will be so for any future occurence. If you take a look at the compiler's assembler listing you will find that the constructor will be compiled to memory only once anyway - unless it's inline'd. In either case, it is exactly the same sequence of assembler mnemonics that are created.
Or let's put it another way: If you write a function, it is either syntactically correct or it isn't. This, however, is not dependant on the number of times it is being called. It will most likely be compiled by the compiler ONCE and remains as a reference in some list to indicate that it does exist and is to be called with a specific list of arguments.
0
 

Expert Comment

by:nke2000
ID: 7113996
fl0yd,
Excellent explanation.  I greatly appreciate the care and time you took to clarify the issue.  I definitely learned a few new things!

--NKE
0
 

Author Comment

by:Kezia
ID: 7118452
fl0yd,

You have interpreted my question correctly.

My question was out of curiosity.

As you have said, the behaviour must be implementation specific to the compiler.

Thanks for the great explanation.
Regards,
Kezia.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question