Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

template based static member functions?

Posted on 2000-03-19
9
Medium Priority
?
181 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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 viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

609 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