By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,003 Members | 1,152 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,003 IT Pros & Developers. It's quick & easy.

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

P: n/a
Is the following appropriate behavior? It certainly isn't what I
expected.
#include <iostream>

using namespace std;

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

}

template<typename Tbool 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
Feb 4 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a


Rick wrote:
Is the following appropriate behavior? It certainly isn't what I
expected.
#include <iostream>

using namespace std;

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

}

template<typename Tbool 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.
Feb 5 '08 #2

P: n/a
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
Feb 5 '08 #3

P: n/a
Rick wrote:
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?
I don't think it is described in one place, but as a combination of
several rules.

The minute details of overload resolution are really so complicated
that probably nobody really understands them fully. They are still
useful though, if you don't push it.

For function overloads, f(T) and f(const T&) are equally good. None of
them requires a conversion, or anything, for an object that is already
of type T. Like the compiler tells you, that means that it cannot
chose one of them as a better fit.

It also doesn't matter if you refer to Robert as Bob, he is still the
same object of the same type.

Bo Persson
Feb 6 '08 #4

P: n/a
Andrey Tarasevich wrote:
What you wrote in your code does not "tell the compiler explicitly" that
you want a "reference" version of the function. To "tell" something like
that you can force overload resolution by taking the function's address
first

(bool (*)(const int&)) fun(10);
Oops... Should be

((bool (*)(const int&)) fun)(10);

--
Best regards,
Andrey Tarasevich
Feb 7 '08 #5

P: n/a
Andrey,

Thanks very much for the standard references. I will peruse them
this evening.

Thanks also for the clarification about how to call a particular
overload explicitly. So to say that in English (I can be a little
laborious) it's: Cast the symbol "fun" to a pointer to a function
that takes a reference to a constant int and returns a bool, and call
the result with 10.
Regards,

Rick
Feb 7 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.