
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!)