Help: Function Overloading Resolution, Which Function?

C

coolpint

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;
}
 
A

Asongala

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
 
M

Marcus Kwok

(e-mail address removed) posted:
by the way what does "const char (&)[15]" mean?

Tom?s said:
A const reference to an array of fifteen "char"'s.

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

bonczz

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

Marcus Kwok

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:

#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;
}

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.

(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] )

You can say:

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

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

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,020
Latest member
GenesisGai

Latest Threads

Top