A problem about template function overload

M

miaohua1982

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

Guest

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.
 
M

miaohua1982

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.

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top