Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 176
  • Last Modified:

Error checking in templates

Does error checking take place for each instantiation, even if the types generated are the same?
0
Kezia
Asked:
Kezia
  • 5
  • 4
  • 3
1 Solution
 
fl0ydCommented:
Could you be a bit more precise? You are talking about c++-templates, right? What error checking exactly are you talking about?
0
 
nke2000Commented:
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
 
fl0ydCommented:
@ nke2000:
This is not necessarily true. Think about assert(), for example, which is only compiled in DEBUG-mode.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
KeziaAuthor Commented:
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
 
fl0ydCommented:
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
 
nke2000Commented:
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
 
KeziaAuthor Commented:
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
 
fl0ydCommented:
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
 
nke2000Commented:
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
 
fl0ydCommented:
@ 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
 
nke2000Commented:
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
 
KeziaAuthor Commented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 5
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now