Solved

Templates and Exceptions MIXED

Posted on 1998-07-14
9
199 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 100 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
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

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…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

832 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