Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Re: value vs. c...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 8 Topic 43093 of 48417
Post > Topic >>

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

by "Bo Persson" <bop@[EMAIL PROTECTED] > Feb 5, 2008 at 12:56 AM

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
 




 8 Posts in Topic:
value vs. const ref, char* vs char[4] - part 2
Rick <CubsNo1@[EMAIL P  2008-02-04 14:58:35 
Re: value vs. const ref, char* vs char[4] - part 2
"Bo Persson" &l  2008-02-05 00:56:43 
Re: value vs. const ref, char* vs char[4] - part 2
diligent.snail@[EMAIL PRO  2008-02-04 16:32:47 
Re: value vs. const ref, char* vs char[4] - part 2
Rick <CubsNo1@[EMAIL P  2008-02-05 10:29:07 
Re: value vs. const ref, char* vs char[4] - part 2
"Bo Persson" &l  2008-02-06 20:49:32 
Re: value vs. const ref, char* vs char[4] - part 2
Andrey Tarasevich <and  2008-02-06 16:50:42 
Re: value vs. const ref, char* vs char[4] - part 2
Andrey Tarasevich <and  2008-02-06 16:54:20 
Re: value vs. const ref, char* vs char[4] - part 2
Rick <CubsNo1@[EMAIL P  2008-02-07 07:20:11 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Nov 21 10:52:58 CST 2008.