Solved

Templates and Exceptions MIXED

Posted on 1998-07-14
9
200 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

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…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 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.

792 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