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;
} 6 1487
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
by the way what does "const char (&)[15]" mean? bo****@gmail.com posted: by the way what does "const char (&)[15]" mean?
A const reference to an array of fifteen "char"'s.
-Tomás
> bo****@gmail.com posted: by the way what does "const char (&)[15]" mean?
"Tom?s" <NU**@null.null> wrote: 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.
--
Marcus Kwok
(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
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: bo****@gmail.com <bo****@gmail.com> wrote: #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. bo****@gmail.com <bo****@gmail.com> wrote: (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 :)
--
Marcus Kwok This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Dave Theese |
last post by:
Hello all,
I'm trying to get a grasp of the difference between specializing a function
template and overloading it. The example below has a primary template, a
specialization and an overload. ...
|
by: Levent |
last post by:
Hi,
Why doesn't this work? (tried with gcc 3.3.3 and VC++ 7.1):
#include <iostream>
template<class T, unsigned N>
struct Foo {
void func();
};
template<class T, unsigned N>
|
by: WittyGuy |
last post by:
Hi,
What is the major difference between function overloading and function
templates?
Thanks!
http://www.gotw.ca/resources/clcm.htm for info about ]
|
by: sjbrown8 |
last post by:
I have the piece of code below, and when i try compiling with the line
g++ 753075304.cpp
I get the following error message:
753075304.cpp: In function 'int main()':
753075304.cpp:29: error:...
|
by: zeus |
last post by:
I know function overloading is not supported in C.
I have a few questions about this:
1. Why? is it from technical reasons? if so, which?
2. why wasn't it introduced to the ANSI?
3. Is there any...
|
by: |
last post by:
Hi,
Why can I not overload on just the return type?
Say for example.
public int blah(int x)
{
}
|
by: flopbucket |
last post by:
Could someone explain to me what the difference is between function
template specialization and function overloading?
I guess overloading can change the number of parameters, but otherwise
they...
|
by: mesut |
last post by:
hi colleagues,
I don't know if this is the right group for but it's in C# so I try.
I have a #3 procedural function called GetInfo.. and those are 3
overloaded methods. I would like to use the...
|
by: matthias.neubauer |
last post by:
I have problems understanding how overloading of function templates
works.
Consider first the following code without any function templates ...
int foo(const char *& c)
{
return 0;
}
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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,...
|
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: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| | |