?
Solved

template based static member functions?

Posted on 2000-03-19
9
Medium Priority
?
182 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 22

Accepted Solution

by:
nietod earned 300 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

862 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