Solved

Templates and Exceptions MIXED

Posted on 1998-07-14
9
196 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
Comment Utility
answer coming.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
Comment Utility
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
 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 11

Expert Comment

by:alexo
Comment Utility
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
Comment Utility
>> 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
Comment Utility
>> 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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
  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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

762 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now