Solved

template based static member functions?

Posted on 2000-03-19
9
169 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
how to understand recursion 12 206
FMX enumerated colours 2 72
c++ syntax question 9 34
Add values of each row in an array 3 33
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 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 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.

759 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

18 Experts available now in Live!

Get 1:1 Help Now