C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Template overloading, error on lookup

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Matthew D Moss
Guest





PostPosted: Wed Aug 16, 2006 2:52 am    Post subject: Template overloading, error on lookup Reply with quote



I'm porting our codebase to a new platform using a build of gcc 4.0.2.
Currently, it compiles under a build of gcc 2.95 and a couple Microsoft
compilers

I'm not sure what the standard makes of the code below; trying to
figure that out now. This code is our own enhanced version of printf;
does type-checking, etc. I didn't write this code here, just trying to
port it, and gcc 4.0.2 is giving me the following error:

foo.cpp: In function 'void bar(int)':
foo.h:46: error: too many arguments to function 'const char*
MakeString(const char*)'
foo.cpp:246: error: at this point in file

Can someone give me a hint, or pointer into the standard, or
explanation of the problem? The older compilers find MakeString(const
char* fmt, T1 t1) just fine, but as I've been finding out, gcc 4.0.2
seems to be stricter in compliance.

Thanks....


// foo.h
class FormatString
{
public:
FormatString(const char* fmt);
FormatString& operator << (int);
FormatString& operator << (const char*);
// etc...
const char* Str();
};

inline const char* MakeString(const char* fmt) // LINE 46
{
FormatString f(fmt);
return f.Str();
}

template<class T1>
const char* MakeString(const char* fmt, T1 t1)
{
FormatString f(fmt);
f << t1;
return f.Str();
}

template<class T1, class T2>
const char* MakeString(const char* fmt, T1 t1, T2 t2)
{
FormatString f(fmt);
f << t1 << t2;
return f.Str();
}

// etc, more versions of MakeString with more parameters

// foo.cpp
void bar(int k)
{
Log << MakeString("Max value is %d\n", k); // LINE 246
}


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Thomas Tutone
Guest





PostPosted: Wed Aug 16, 2006 4:41 am    Post subject: Re: Template overloading, error on lookup Reply with quote



Matthew D Moss wrote:

Quote:
I'm porting our codebase to a new platform using a build of gcc 4.0.2.
Currently, it compiles under a build of gcc 2.95 and a couple Microsoft
compilers

I'm not sure what the standard makes of the code below; trying to
figure that out now. This code is our own enhanced version of printf;
does type-checking, etc. I didn't write this code here, just trying to
port it, and gcc 4.0.2 is giving me the following error:

foo.cpp: In function 'void bar(int)':
foo.h:46: error: too many arguments to function 'const char*
MakeString(const char*)'
foo.cpp:246: error: at this point in file

Can someone give me a hint, or pointer into the standard, or
explanation of the problem? The older compilers find MakeString(const
char* fmt, T1 t1) just fine, but as I've been finding out, gcc 4.0.2
seems to be stricter in compliance.


[code snipped]

Other than the fact that "Log" is undefined, Comeau online compiles
your code without any diagnostic. I think your code is well-formed.
gcc 4.0.2 is quite buggy. Try it on gcc 4.1.1 or on gcc 3.4.6 (which
is quite stable) and see if you have the same problem.

Best regards,

Tom


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Matthew D Moss
Guest





PostPosted: Wed Aug 16, 2006 7:11 am    Post subject: Re: Template overloading, error on lookup Reply with quote



Turns out I was missing the appropriate FormatString::operator<< for
the actual type being passed into MakeString. (My example showed an int
to simplify the code, but it was actually an enum.)

Typecasting the enum to an int allowed the template function to be
found.

Although I guess I'm wondering whether that's just a misunderstanding
on my part (of the error msg), a misleading error msg, or something
really broken.

I can't really upgrade from 4.0.2 right at this moment... but thanks
for the mention about that version. I may look to see if our provider
is going to move up.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Earl Purple
Guest





PostPosted: Thu Aug 17, 2006 3:14 am    Post subject: Re: Template overloading, error on lookup Reply with quote

Matthew D Moss wrote:
Quote:

// foo.h
class FormatString
{
public:
FormatString(const char* fmt);
FormatString& operator << (int);
FormatString& operator << (const char*);
// etc...
const char* Str();
};

inline const char* MakeString(const char* fmt) // LINE 46
{
FormatString f(fmt);
return f.Str();
}

template<class T1
const char* MakeString(const char* fmt, T1 t1)
{
FormatString f(fmt);
f << t1;
return f.Str();
}

template<class T1, class T2
const char* MakeString(const char* fmt, T1 t1, T2 t2)
{
FormatString f(fmt);
f << t1 << t2;
return f.Str();
}

// etc, more versions of MakeString with more parameters

Presumably FormatString creates a char* parameter with new and then
doesn't call delete[] in its destructor or the pointer the Str()
function returns would be dangling. Or it duplicates the pointer in its
Str() command or relinquishes ownership to it.

I will also assume this is legacy code.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Matthew D Moss
Guest





PostPosted: Thu Aug 17, 2006 8:49 pm    Post subject: Re: Template overloading, error on lookup Reply with quote

Quote:
Presumably FormatString creates a char* parameter with new and then
doesn't call delete[] in its destructor or the pointer the Str()
function returns would be dangling. Or it duplicates the pointer in its
Str() command or relinquishes ownership to it.

Neither. There is a global array:

char gStrings[NUM_STRS][STR_SIZE];

As each FormatString object is constructed, an index into this array is
incremented (with modulo), to avoid stomping on the results of the last
call to MakeString. The constants (uhh, macros) NUM_STRS and STR_SIZE
have been determined empirically.


Quote:
I will also assume this is legacy code.

Uhh, not exactly. Not really the way I would have done it, but the
leads have their ways with regards to what they perceive as needing to
be fast. (Game code, and yes, I said "perceive" not "measure".)
Certainly not truly safe, but in practice has worked well enough, the
cost being a bit of debugging time when someone would exceed the
NUM_STRS limit, in which case it would get bumped up a notch or two.

I don't go changing because (a) they might kill me, and (b) I have to
figure out this durned PS3. :)


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.