Solved

Error checking in templates

Posted on 2002-06-23
12
172 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
Technology Partners: 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

740 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