Solved

template based static member functions?

Posted on 2000-03-19
9
171 Views
Last Modified: 2010-04-02
I'm writing some exception handling classes, and am getting an error compiling a template based static member function:

class CErr{
    CErr(LPTSTR lpErr){...}
    CErr(DWORD dwErr){...}

    template<class T, class ARG>
    static void Throw(ARG argErr){
        throw T(argErr);
    }
};

In VC6 with SP3, using the function like this:

CErr::Throw<CErrDb>(_T("USER NOT FOUND"));

Causes this error:

C:\Server\Db.cpp(54) : fatal error C1001: INTERNAL COMPILER ERROR
        (compiler file 'msc1.cpp', line 1794)
         Please choose the Technical Support command on the Visual C++
         Help menu, or open the Technical Support help file for more information
Error executing cl.exe.


Any Ideas?
Jon Stelly

P.S.  If the formatting is screwed up here, I'm going to scream. I tried another website, and it screwed the formatting to hell.
0
Comment
Question by:jonpants
  • 6
  • 3
9 Comments
 

Author Comment

by:jonpants
ID: 2634002
Ah, looks like the formatting is correct, but I forgot a detail, CErrDb is a subclass of CErr.

The idea is that all of these classes will have a virtual function that handles the error and reports it or silently deals with it.
0
 

Author Comment

by:jonpants
ID: 2634006
Ah, looks like the formatting is correct, but I forgot a detail, CErrDb is a subclass of CErr.

The idea is that all of these classes will have a virtual function that handles the error and reports it or silently deals with it.
0
 

Author Comment

by:jonpants
ID: 2634037
Ah, looks like the formatting is correct, but I forgot a detail, CErrDb is a subclass of CErr.

The idea is that all of these classes will have a virtual function that handles the error and reports it or silently deals with it.
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 2634072
The problem is that VC does not officially support member template functions.  i.e. VC does not allow you to have member functions of a class that are template functions.  This is allowed by the C++ standard, but VC does not yet support it.  Although it simple cases template member functions may compile and work correctly, the VC docs specifically warn against relying on them.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2634074
If you want proof, I can post a clipping or two from the VC docs about it.  I was about to do so, but just realized I cannot at the moment, as my C++ workstation is occupied, and can't be used for an hour or two.   Otherwise, you could just trust me on it.  :-)
0
 

Author Comment

by:jonpants
ID: 2634082
Wow, I was expecting the answer to explain that I was just doing something wrong.  I can't say I'm happy though, I'll have to rethink my error handling code now.

Thanks Nietod.
0
 

Author Comment

by:jonpants
ID: 2634098
I trust ya, The error message makes it look like the compiler is choking trying to parse it which means you're probably right and that it's just not supported yet.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2634113
Well, to being with, at least from what I've seen of your code it doesn't seem like there is much reason to have the function in the first place.  Why call a function whose job is to throw an exception?  Just throw the exception directly.  If the function did other things as well, then it might make sense....  Does it?

If so, you could make the Throw() function a non-template function and have it take as a parameter the object that needs to be thrown.  since there appears to be more than 1 type of object to be throw, you either need to declare them in a class hierarchy ahd have the Throw() function take a reference to the base class type or you need to write multiple overloads for the Throw() function.  (Which may lead to some duplicate code, or additional function calls to eliminate duplication.)
0
 

Author Comment

by:jonpants
ID: 2634127
Right, it's for a server application where I'd like to stop accepting new activity on the object that is throwing the error.  I was going to use the throw function to set a flag on the object which marks it as inactive, I had just removed that portion of the code to make sure I wasn't doing something stupid.  I'll just handle the error in the catch section.  After thinking about it, that makes more sense anyway.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

  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 …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

776 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