|
From: Ulrich Eckhardt on 18 Apr 2008 07:20 Fabian wrote: > is there any possibility to define default parameters? Default parameters > for templates are the default types, right? But if I could do sth. like > > template void PrintError(const char*, const char* = 0, const char* = 0, > const char* = 0, const char* = 0); If you already know that you will only be logging strings, you don't need any template, just an overloaded function with default argument like above. Otherwise, my compiler claims that I can't use default template parameters (_not_ functions parameters!) with template functions. > for the specialization in the cpp file, I wouldn't have to write > specializations for a different number of parameters. But the compiler > (btw: I'm quite up to date: Visual Studio 2008) doesn't like it: > > 'PrintError' : an explicit specialization of a function template cannot > have any default arguments Well, of course you are not repeating the default arguments, right? You never do that when you define a function that was declared with default arguments. Uli -- C++ FAQ: http://parashift.com/c++-faq-lite Sator Laser GmbH Geschäftsführer: Michael Wöhrmann, Amtsgericht Hamburg HR B62 932
From: Fabian on 18 Apr 2008 08:37 Hello Uli, > If you already know that you will only be logging strings, you don't need > any template, just an overloaded function with default argument like above. I am not sure that I'm always logging strings. I wrote overloads for the template functions now - with a different number of arguments each: // header file: template <typename T1> COMMON_API inline void PrintError(const T1& arg1); template <typename T1, typename T2> COMMON_API inline void PrintError(const T1& arg1, const T2& arg2); template <typename T1, typename T2, typename T3> COMMON_API inline void PrintError(const T1& arg1, const T2& arg2, const T3& arg3); // and so on... // cpp file: template <typename T1> void PrintError(const T1& arg1) { cerr << arg1 << endl; } template <typename T1, typename T2> void PrintError(const T1& arg1, const T2& arg2) { cerr << arg1 << ": " << arg2 << endl; } template <typename T1, typename T2, typename T3> void PrintError(const T1& arg1, const T2& arg2, const T3& arg3) { cerr << arg1 << ": " << arg2 << ", " << arg3 << endl; } // and so forth... But when I now compile a specialization with more than one parameter I get a compiler error: template inline void COMMON_API PrintError<const char*>(const char*); // works template inline void COMMON_API PrintError<const char*, const char*>(const char*, const char*); // produces C2977 error C2977: 'PrintError' : too many template arguments So what is this now??? Thanks for your patience with me, Fabian
From: Fabian on 18 Apr 2008 08:38 Hello Uli, > If you already know that you will only be logging strings, you don't need > any template, just an overloaded function with default argument like above. I am not sure that I'm always logging strings. I wrote overloads for the template functions now - with a different number of arguments each: // header file: template <typename T1> COMMON_API inline void PrintError(const T1& arg1); template <typename T1, typename T2> COMMON_API inline void PrintError(const T1& arg1, const T2& arg2); template <typename T1, typename T2, typename T3> COMMON_API inline void PrintError(const T1& arg1, const T2& arg2, const T3& arg3); // and so on... // cpp file: template <typename T1> void PrintError(const T1& arg1) { cerr << arg1 << endl; } template <typename T1, typename T2> void PrintError(const T1& arg1, const T2& arg2) { cerr << arg1 << ": " << arg2 << endl; } template <typename T1, typename T2, typename T3> void PrintError(const T1& arg1, const T2& arg2, const T3& arg3) { cerr << arg1 << ": " << arg2 << ", " << arg3 << endl; } // and so forth... But when I now compile a specialization with more than one parameter I get a compiler error: template inline void COMMON_API PrintError<const char*>(const char*); // works template inline void COMMON_API PrintError<const char*, const char*>(const char*, const char*); // produces C2977 error C2977: 'PrintError' : too many template arguments So what is this now??? Thanks for your patience with me, Fabian
First
|
Prev
|
Pages: 1 2 3 Prev: problem using delphi dll in vc++ Next: A Windows object named "Instance"... |