?
Solved

Error checking in templates

Posted on 2002-06-23
12
Medium Priority
?
175 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 400 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

649 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