Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > A problem about template function overload

Reply
Thread Tools

A problem about template function overload

 
 
miaohua1982@gmail.com
Guest
Posts: n/a
 
      03-18-2007
the code as follows:
#include<iostream>
using namespace std;

template <int N>
void foo( const char (&str)[N])
{
cout<<"array"<<endl;
}
template <typename T>
void foo(const T& str);
template <>
void foo(char *const &str)
{
cout<<"char *"<<endl;
}

int main()
{
char arr[10];
foo(arr);
}

why the called function is the void foo(const char(&str)[N])? I test
the code by VC7, the output is
*array*, but according to my knowledge, the following code is also
OK:

char p[1];
char * const & str = p;

so why the output is not *char*? I mean the why the *exact match*
funcion is not the secnod "foo" with params (char *const &str)?

It has confused me so much. Is there anyone call tell me?
Thank you very much!

 
Reply With Quote
 
 
 
 
=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
Guest
Posts: n/a
 
      03-18-2007
On 2007-03-18 14:02, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> the code as follows:
> #include<iostream>
> using namespace std;
>
> template <int N>
> void foo( const char (&str)[N])
> {
> cout<<"array"<<endl;
> }
> template <typename T>
> void foo(const T& str);
> template <>
> void foo(char *const &str)
> {
> cout<<"char *"<<endl;
> }
>
> int main()
> {
> char arr[10];
> foo(arr);
> }
>
> why the called function is the void foo(const char(&str)[N])? I test
> the code by VC7, the output is
> *array*, but according to my knowledge, the following code is also
> OK:
>
> char p[1];
> char * const & str = p;
>
> so why the output is not *char*? I mean the why the *exact match*
> funcion is not the secnod "foo" with params (char *const &str)?


Because an array is not a pointer, it can however decay (is that the
correct word?) to a pointer, so the function taking an array is a better
match since no conversion is needed.

--
Erik Wikström
 
Reply With Quote
 
 
 
 
miaohua1982@gmail.com
Guest
Posts: n/a
 
      03-19-2007
On 3月18日, 下午10时28分, Erik Wikström <(E-Mail Removed)> wrote:
> On 2007-03-18 14:02, (E-Mail Removed) wrote:
>
>
>
>
>
> > the code as follows:
> > #include<iostream>
> > using namespace std;

>
> > template <int N>
> > void foo( const char (&str)[N])
> > {
> > * * * cout<<"array"<<endl;
> > }
> > template <typename T>
> > void foo(const T& str);
> > template <>
> > void foo(char *const &str)
> > {
> > * *cout<<"char *"<<endl;
> > }

>
> > int main()
> > {
> > * * char arr[10];
> > * *foo(arr);
> > }

>
> > why *the called function is the *void foo(const char(&str)[N])? I test
> > the code by VC7, the output is
> > *array*, *but according to my knowledge, the following code is also
> > OK:

>
> > char p[1];
> > char * const & str = p;

>
> > so why the output is not *char*? I mean the why the *exact match*
> > funcion is not the secnod "foo" with params (char *const &str)?

>
> Because an array is not a pointer, it can however decay (is that the
> correct word?) to a pointer, so the function taking an array is a better
> match since no conversion is needed.
>
> --
> Erik Wikström- 隐藏被引用文* -
>
> - 显示引用的文* -


well, I don't think so. Just have a look at the following code:
#include<iostream>
using namespace std;

template <int N>
void foo( const char (&str)[N])
{
cout<<"array"<<endl;
}

void foo(char * const &str)
{
cout<<"char *"<<endl;
}

int main()
{
char arr[10];
foo(arr);
}

the output in VC7 is "char*", so can you explain it?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use the template member function of a template in the memberfunction of another template class? Peng Yu C++ 3 10-26-2008 03:51 PM
function template and operator () overload Fei Liu C++ 5 02-25-2006 12:28 AM
function overload (not operator overload) Ying-Chieh Liao Perl Misc 3 10-11-2004 11:24 AM
How use the overload of>> (or<<) of a class in the overload of << and >> of another class? Piotre Ugrumov C++ 3 01-25-2004 08:08 PM
Re: Template member function overload Victor Bazarov C++ 0 07-31-2003 04:15 PM



Advertisments