value vs. const ref, char* vs char[4] - part 2



 DEVELOP > c-Plus-Plus > value vs. const ref, char* vs char[4] - part 2

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Rick"
Date: 04 Feb 2008 04:58:35 PM
Object: value vs. const ref, char* vs char[4] - part 2
Is the following appropriate behavior? It certainly isn't what I
expected.
#include <iostream>
using namespace std;
template<typename T> bool fun(const T& value) {
cout << "In fun(const T&);" << endl;
}
template<typename T> bool fun(T value) {
cout << "In fun(T);" << endl;
}
int main(int argc, char** argv) {
fun(static_cast<const int&>(10) );
}
$ CC test.C
"test.C", line 14: Error: Overloading ambiguity between
"fun<int>(const int&)" and "fun<int>(int)".
1 Error(s) detected.
$
$
$ g++ test.C
test.C: In function `int main(int, char**)':
test.C:14: error: call of overloaded `fun(const int&)' is ambiguous
test.C:5: note: candidates are: bool fun(const T&) [with T = int]
test.C:9: note: bool fun(T) [with T = int]
$
I would have said that if I tell the compiler explicitly that I want a
const int&, there is no ambiguity. Something in the C++ standard
apparently says that this is correct behavior, but it seems wrong to
me.
Regards,
Abraham
.

User: ""

Title: Re: value vs. const ref, char* vs char[4] - part 2 04 Feb 2008 06:32:47 PM
Rick wrote:

Is the following appropriate behavior? It certainly isn't what I
expected.


#include <iostream>

using namespace std;

template<typename T> bool fun(const T& value) {
cout << "In fun(const T&);" << endl;

}

template<typename T> bool fun(T value) {
cout << "In fun(T);" << endl;

}

int main(int argc, char** argv) {
fun(static_cast<const int&>(10) );

}

$ CC test.C
"test.C", line 14: Error: Overloading ambiguity between
"fun<int>(const int&)" and "fun<int>(int)".
1 Error(s) detected.
$
$
$ g++ test.C
test.C: In function `int main(int, char**)':
test.C:14: error: call of overloaded `fun(const int&)' is ambiguous
test.C:5: note: candidates are: bool fun(const T&) [with T = int]
test.C:9: note: bool fun(T) [with T = int]
$

I would have said that if I tell the compiler explicitly that I want a
const int&, there is no ambiguity. Something in the C++ standard
apparently says that this is correct behavior, but it seems wrong to
me.

Regards,

Abraham

It seems to me too that the first template function is more
especialized, and should be prefered in overload resolution.
I tried the standard and did not manage to find explanation for the
compiler's behaviour.
Howvere, I tried your code with the Cameau compiler and it compiled
successfully:
http://www.comeaucomputing.com/tryitout/
So is it a compiler bug? Really do not know.
(by the way, which version of g++ are you using?)
Regards.
.
User: "Rick"

Title: Re: value vs. const ref, char* vs char[4] - part 2 05 Feb 2008 12:29:07 PM
I tried to answer a little while ago and appear to have hit the wrong
button. Oh well.
Mr. Persson: Thanks for your explanation. It helps some. Do you
know where this behavior is described in the Standard?
Regards,
Abraham
.


User: "Bo Persson"

Title: Re: value vs. const ref, char* vs char[4] - part 2 04 Feb 2008 05:56:43 PM
Rick wrote:

Is the following appropriate behavior? It certainly isn't what I
expected.

So your expectations are wrong. :-)



#include <iostream>

using namespace std;

template<typename T> bool fun(const T& value) {
cout << "In fun(const T&);" << endl;

}

template<typename T> bool fun(T value) {
cout << "In fun(T);" << endl;

}

int main(int argc, char** argv) {
fun(static_cast<const int&>(10) );

}

$ CC test.C
"test.C", line 14: Error: Overloading ambiguity between
"fun<int>(const int&)" and "fun<int>(int)".
1 Error(s) detected.
$
$
$ g++ test.C
test.C: In function `int main(int, char**)':
test.C:14: error: call of overloaded `fun(const int&)' is ambiguous
test.C:5: note: candidates are: bool fun(const T&) [with T = int]
test.C:9: note: bool fun(T) [with T = int]
$

I would have said that if I tell the compiler explicitly that I
want a const int&, there is no ambiguity. Something in the C++
standard apparently says that this is correct behavior, but it
seems wrong to me.

Your problem is that there is not such thing as "a const int&",
because that is just a reference to an object that exists elsewhere.
It can't exist by itself.
Try this instead, assuming a class Person:
Person Robert; // A person called Robert
Person& Bob = Robert; // Another name for Robert
fun(Robert);
fun(Bob);
Do you want different overloads called depending on what name we use
for the same person? No!
Now this:
const number ten = 10;
const number& also_ten = ten;
fun(ten);
fun(also_ten);
Do you want different overloads called depending on what name we use
for the number 10? Not really!
Bo Persson
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER