Solved

template based static member functions?

Posted on 2000-03-19
9
170 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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 …
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 …
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 technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

863 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

22 Experts available now in Live!

Get 1:1 Help Now