Solved

Error checking in templates

Posted on 2002-06-23
12
166 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
  • 5
  • 4
  • 3
12 Comments
 
LVL 8

Expert Comment

by:fl0yd
Comment Utility
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
Comment Utility
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
Comment Utility
@ nke2000:
This is not necessarily true. Think about assert(), for example, which is only compiled in DEBUG-mode.
0
 

Author Comment

by:Kezia
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:Kezia
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
@ 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
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now