473,387 Members | 1,528 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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!

Mar 18 '07 #1
2 1263
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Paul MG | last post by:
Hi Template partial specialization always seems like a fairly straightforward concept - until I try to do it :). I am trying to implement the input sequence type (from Stroustrup section...
5
by: wongjoekmeu | last post by:
Hello all, I have a question about templates. Let say I have the following template function ----- template <class T> T max(T a, T b) { return a > b ? a : b ; }
14
by: Bart Samwel | last post by:
Hi everybody, I would really like some help explaining this apparent discrepancy, because I really don't get it. Here is the snippet: void foo(int&); void foo(int const&); ...
1
by: Dave Corby | last post by:
Hi all, I have an overloaded template function, and in one particular spot can't get the right version of it to be called. Everywhere else in the program the correct version is called. Here's...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
5
by: Allen | last post by:
Yesterday I discussed with my boss about method naming. In the RPC argument pack / unpack classes, packing & unpacking remote procedure calling arguments into / from a byte buffer. I named the...
8
by: Rahul | last post by:
Hi, Is there a way to partially specialize only a member function of a template class (not the whole class). e.g. template <typename A, typename B> class Base { public:
7
by: markscottwright | last post by:
I'm using visual studio 8, and the following code is failing. For the life of me, I can't see what's wrong... void myTest(std::string const& in) { using namespace std; string::const_iterator...
9
by: neildferguson | last post by:
I am using templates with a little project I am working on. My compiler (GCC) is finding a particular construct ambiguous. Can anyone suggest something I might change in the declaration of class...
7
by: mathieu | last post by:
Hi there, I know this is not possible in c++. So my question, how should I rewrite the following piece of code (without using a dummy class which template parameter could be use for partial...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.