a template function question 
Author Message
 a template function question

I define a template function as followed:
template <class T> int Comp(const T& Value1,const T& Value2)
{
  if (Value1==Value2)
    return 0;
  if (Value1>Value2)
    return 1;
  else
    return -1;
Quote:
}

and then I overload it with a non-template version:
int Comp(const char* Value1,const char* Value2)
{
  return strcmp(Value1,Value2);

Quote:
}

But when I call Comp("abc","def"), it always call the template version Comp.
What's wrong with that? Please tell me and thank you!
(But I test above piece of code under C++Builder and find it work right!)


Tue, 14 Oct 2003 09:17:45 GMT  
 a template function question

Owen,
        I don't know what to tell you.  I copied your code exactly from your message
and pasted it into a test program.  I set a breakpoint on the
"Comp("abc","def")" call, and it stepped into the override version, exactly as
it should.  I am using VC++ 6.0 with service pack 5.  What do you have?

  marsco3.vcf
< 1K Download


Tue, 14 Oct 2003 10:08:38 GMT  
 a template function question
I use VC++4.1. But I think it is a basic standard to a compiler,so it should
be not relatively to compiler's version. Do you think so?


Quote:
> Owen,
> I don't know what to tell you.  I copied your code exactly from your
message
> and pasted it into a test program.  I set a breakpoint on the
> "Comp("abc","def")" call, and it stepped into the override version,
exactly as
> it should.  I am using VC++ 6.0 with service pack 5.  What do you have?



Wed, 15 Oct 2003 14:29:08 GMT  
 a template function question
Visual C++ 4.1 was released in 96, nearly 2 years before the C++ standard
was finalized.  Why would you expect a compiler written before the standard
to support the standard?


Quote:
> I use VC++4.1. But I think it is a basic standard to a compiler,so it
should
> be not relatively to compiler's version. Do you think so?


> > Owen,
> > I don't know what to tell you.  I copied your code exactly from your
> message
> > and pasted it into a test program.  I set a breakpoint on the
> > "Comp("abc","def")" call, and it stepped into the override version,
> exactly as
> > it should.  I am using VC++ 6.0 with service pack 5.  What do you have?



Wed, 15 Oct 2003 17:16:50 GMT  
 a template function question
I definitely suggest you upgrade.  Version 6.0 is much, much better, but even
5.0 was very acceptable.


Thu, 16 Oct 2003 02:55:21 GMT  
 a template function question
Especially when even the ones written after the standard
don't support the standard!    :-(


Quote:
> Visual C++ 4.1 was released in 96, nearly 2 years before the C++ standard
> was finalized.  Why would you expect a compiler written before the
standard
> to support the standard?



> > I use VC++4.1. But I think it is a basic standard to a compiler,so it
> should
> > be not relatively to compiler's version. Do you think so?


> > > Owen,
> > > I don't know what to tell you.  I copied your code exactly from your
> > message
> > > and pasted it into a test program.  I set a breakpoint on the
> > > "Comp("abc","def")" call, and it stepped into the override version,
> > exactly as
> > > it should.  I am using VC++ 6.0 with service pack 5.  What do you
have?



Sat, 18 Oct 2003 01:27:34 GMT  
 a template function question
    Because in C, we didn't have "const" for many years.

So many of the C standard library functions were defined like:
    strcpy(char*, char*);
when everyone knows it should really be "strcpy(char*, const char*)"

Now, many C++ compilers are written to produce C code which is then compiled
with a downstream C compiler (VC++ isn't one, but it has to follow the same
rules).  These C++ compilers use the host C compiler's C header files when
one is #include'd in a C++ program.  So, if you used 'strcpy(buff, "Hello,
World!");' in your C++ code, you might have been using a C header which
defined strcpy as strcpy(char*, char*).  So, if your compiler treated
"Hello, World!" as "const char*", you would have gotten a compiler error.

To solve this, the C Standard, and for a while, the C++ Draft Standard
declared that a text constant is a "char*", not a "const char*".

Eventually, the C++ Standard committee came up with a better solution: A
text constant is a "const char*" but it can be silently converted to a "char
*"

Your compiler is apparently still working under the old rules, where
Comp("abc", "def") will match Comp(char*, char*) instead of the overload you
give.  As Scott points out, newer versions of VC++ correctly reflect the new
rule.

--
Truth,
James Curran
www.NJTheater.com     (Professional)
www.NovelTheory.com  (Personal)
www.BrandsForLess.Com (Day Job)


Quote:
> I define a template function as followed:
> template <class T> int Comp(const T& Value1,const T& Value2)
> {
>   if (Value1==Value2)
>     return 0;
>   if (Value1>Value2)
>     return 1;
>   else
>     return -1;
> }
> and then I overload it with a non-template version:
> int Comp(const char* Value1,const char* Value2)
> {
>   return strcmp(Value1,Value2);
> }

> But when I call Comp("abc","def"), it always call the template version
Comp.
> What's wrong with that? Please tell me and thank you!
> (But I test above piece of code under C++Builder and find it work right!)



Sun, 19 Oct 2003 00:41:17 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Template Function Question????

2. template function question

3. Simple question: Member function templates in template classes under VC++ 5.0

4. Further question on template function exporting

5. function template question

6. template function dependency question

7. Function templates question

8. C++ compiler bug: template and non template function overload resolution

9. template class with a template member function

10. Template function within Template Class

11. template functions in template classes declared outside the class definition

12. pb with template function in non template class

 

 
Powered by phpBB® Forum Software