Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   A problem about template function overload (http://www.velocityreviews.com/forums/t485158-a-problem-about-template-function-overload.html)

miaohua1982@gmail.com 03-18-2007 01:02 PM

A problem about template function overload
 
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!


=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?= 03-18-2007 02:28 PM

Re: A problem about template function overload
 
On 2007-03-18 14:02, miaohua1982@gmail.com 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

miaohua1982@gmail.com 03-19-2007 01:57 AM

Re: A problem about template function overload
 
On 3月18日, 下午10时28分, Erik Wikström <Erik-wikst...@telia.com> wrote:
> On 2007-03-18 14:02, miaohua1...@gmail.com 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?



All times are GMT. The time now is 10:45 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.