[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Templates and Exceptions MIXED

Posted on 1998-07-14
9
Medium Priority
?
207 Views
Last Modified: 2010-04-01
I want to write a template class that one of it's methods
contain try - except (Win32 exceptions) statment.
the Bc5.02 compiler issues a warning "inline functions
cant contain try statment".
so I tryed to move this method to cpp, resulted in
linker error "Unresolved external" (in the files using
the template)
I think that templates has to be in header files only
Is that true? also I have to get rid of the warning
cause I'm afraid it will destroy the precompiled headers.
Templates and Exceptions Don't Mix ?


0
Comment
Question by:gidon
  • 5
  • 4
9 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 400 total points
ID: 1167727
answer coming.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1167728
Make the template function non-inline by moving its definition out of the class's defintion, but leave it in the header file, like

template<T>
class SomeClass
{
    T SomeFunction(T i);
}

Template<T>
T SomeClass::SomeFunction(T i)
{
   cout << i;
   return i;
}
0
 
LVL 11

Expert Comment

by:alexo
ID: 1167729
The problem is that in order to instantiate the template, the compiler has to see the definition code.

Here's an alternative suggestion:

In one source file force the instantiation of the template using:
   template MyTemplateClass<args>;

In other source files prevent the instantiation of the template using:
   extern template MyTemplateClass<args>;

Let the linker sort out the references.
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.

 
LVL 22

Expert Comment

by:nietod
ID: 1167730
The two approaches should produce the same results.  (Notice the "should"  always best to hedge one's bets when at the forefront of C++ technology.)  The method I proposed is what the standard template library (STL) so it is likely to work well.  (Although if you told me if has the side effect of causing massive power outages in Easter Europe, I wouldn't be surprised.  This stuff can be a little unpredicatable.)
0
 
LVL 11

Expert Comment

by:alexo
ID: 1167731
Premature posting...  Brain fart...  Sorry...

However, in your method, there will actually multiple definitions of the class methods which the linker will later merge.  Same result, different way to get there.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1167732
>> which the linker will later merge
Hopefully.   I'm not sure that is guaranteed by the standard.

However, your approach will problably end up working the same.  This is implimentation defined of course, but from what I've heard (I believe--I may be making this up.) it is common to just instanciate every template that is needed in a translation unit.  Then let the linker sort things out.  This is because the output of translation unit (obj) could be linked with different translation units at different times.  Thus an algorithm that looks at compile time at the things that it links with to decide if a procedure should be instanciated (or if it was already instanciated in anther translation unit) will possible fail if you link with something new.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1167733
>> it is common to just instanciate every template that is needed in a translation unit.
Yup.  That's exactly what "my" approach tries to avoid.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1167734
You missunderstand me.  My point is that no matter what you do.  The compiler will instanciate every template in needs in that translation unit, even if it can find out that the template has already been instanciated for the project.  In other words, if you provide the compiler with the declation, but not the definition, it will seek out the definition anyways.  

I don't know how true that is....
0
 
LVL 11

Expert Comment

by:alexo
ID: 1167735
Bzzzzt...  Wrong!  Thank you for participating :-)
Most current compilers will not instantiate using the method I outlined above.  It allows putting pre-instantiated templates in libraries.
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

834 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