By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,797 Members | 1,836 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

A problem about template function overload

P: n/a
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!

Mar 18 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On 2007-03-18 14:02, mi*********@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
Mar 18 '07 #2

P: n/a
On 3月18日, 下午10时28分, Erik Wikström <Erik-wikst...@telia.comwrote:
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?

Mar 19 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.