Connecting Tech Pros Worldwide Help | Site Map

Help: Function Overloading Resolution, Which Function?

  #1  
Old March 27th, 2006, 01:15 PM
coolpint@yahoo.co.uk
Guest
 
Posts: n/a
I am yet again confused about the process of overload resolution.

Can anyone kindly explain why the function #2 is selected over #1 when
passed 'char[15]'?

When passed 'const char[15]', the compiler complains of ambiguity as I
expected.
I was expecting to hear the same complaint when passing 'char[15]'
because I thought both required 'const qualification', but my compiler
(gcc 3.4.2) calls function #2.

So I definitely must be misunderstanding something. Please help me
understand what is going on. Thank you so much in advance!

#include <iostream>
using std::cout;

void which(const char* a, const char* b) { // #1
cout << "const char * ";
}

void which(const char (&)[15], const char (&)[15]) { // #2
cout << "const char (&)[15] ";
}

int main()
{
const char a[15] = {"Whatever"};
const char b[15] = {"Whatever"};
// which(a,b); // overload resolution failure as expected...

char c[15];
char d[15];
which(c,d); // Why does it select the "const char (&)[15]" ?
return 0;
}

  #2  
Old March 27th, 2006, 05:05 PM
Asongala@gmail.com
Guest
 
Posts: n/a

re: Help: Function Overloading Resolution, Which Function?


I think that in"const char a[15]" a is a type of const char* or const
array, the para of two functions is ok.
In "char a[15]" a is a type of char* or array.
Do you think a type of "char*" can conceal to a type of "const char*"?
Change the #1 below, "which(c,d)" will be failure.
Example:
void which(char* const a, char* const b) { // #1
cout << "const char * ";



} //So the char* can conceal to char* const

  #3  
Old March 27th, 2006, 05:25 PM
bonczz@gmail.com
Guest
 
Posts: n/a

re: Help: Function Overloading Resolution, Which Function?


by the way what does "const char (&)[15]" mean?

  #4  
Old March 27th, 2006, 05:55 PM
Tomás
Guest
 
Posts: n/a

re: Help: Function Overloading Resolution, Which Function?


bonczz@gmail.com posted:
[color=blue]
> by the way what does "const char (&)[15]" mean?
>
>[/color]

A const reference to an array of fifteen "char"'s.

-Tomás
  #5  
Old March 27th, 2006, 06:28 PM
Marcus Kwok
Guest
 
Posts: n/a

re: Help: Function Overloading Resolution, Which Function?


> bonczz@gmail.com posted:[color=blue][color=green]
>> by the way what does "const char (&)[15]" mean?
>>
>>[/color][/color]

"Tom?s" <NULL@null.null> wrote:[color=blue]
> A const reference to an array of fifteen "char"'s.[/color]

Actually, it is a reference to an array of fifteen "const char"s. The
"const" applies to the chars, so they cannot be changed. All references
are const since they cannot be reseated, so saying a "const reference"
is redundant.

--
Marcus Kwok
  #6  
Old March 27th, 2006, 07:05 PM
bonczz@gmail.com
Guest
 
Posts: n/a

re: Help: Function Overloading Resolution, Which Function?


(0) Then how can I give it a name? because "const char (&)[15] array"
does not work
and I need a name, for example I want to ask what is its 4th element (
array[3] )
(1) And why I need the semicolons (I need because it does not compile
without them)?
|'m looking forward the answer. thanks

  #7  
Old March 27th, 2006, 07:16 PM
Marcus Kwok
Guest
 
Posts: n/a

re: Help: Function Overloading Resolution, Which Function?


Hi bonczz,
First, please read http://cfaj.freeshell.org/google/ to see how you
should quote context in your replies.


Going back to the original code:

bonczz@gmail.com <bonczz@gmail.com> wrote:[color=blue]
> #include <iostream>
> using std::cout;
>
> void which(const char* a, const char* b) { // #1
> cout << "const char * ";
> }
>
> void which(const char (&array)[15], const char (&)[15]) { // #2
> cout << "const char (&)[15] ";
> }
>
> int main()
> {
> const char a[15] = {"Whatever"};
> const char b[15] = {"Whatever"};
> // which(a,b); // overload resolution failure as expected...
>
> char c[15];
> char d[15];
> which(c,d); // Why does it select the "const char (&)[15]" ?
> return 0;
> }[/color]

Using Visual Studio .NET 2003, which is also known as
Visual C++ .NET 7.1, I get the following compiler error:

test.cpp
test.cpp(20) : error C2668: 'which' : ambiguous call to overloaded function
test.cpp(8): could be 'void which(const char (&)[15],const char (&)[15]) '
test.cpp(4): or 'void which(const char *,const char *)'
while trying to match the argument list '(char [15], char [15])'

However, Comeau online compiles it. Someone who has better knowledge of
the function overload resolution rules will have to elaborate.

bonczz@gmail.com <bonczz@gmail.com> wrote:[color=blue]
> (0) Then how can I give it a name? because "const char (&)[15] array"
> does not work
> and I need a name, for example I want to ask what is its 4th element (
> array[3] )[/color]

You can say:

const char (&array)[15]
[color=blue]
> (1) And why I need the semicolons (I need because it does not compile
> without them)?[/color]

I'm not sure which semicolons you are talking about. C++ needs a lot of
semicolons :)

--
Marcus Kwok
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function Overloading Pramod answers 14 June 19th, 2007 08:25 AM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 14th, 2005 03:55 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 5 November 14th, 2005 12:36 PM
I need some basic C++ help Psykarrd answers 41 July 22nd, 2005 04:36 AM