473,378 Members | 1,639 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 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
5 1829


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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Thomas Matthews | last post by:
Hi, Given a structure of pointers: struct Example_Struct { unsigned char * ptr_buffer; unsigned int * ptr_numbers; }; And a function that will accept the structure:
17
by: sounak | last post by:
How could we get a macro name from a macro value such that in a header file #define a 100 #define b 200 now the source file will be such that the user gives 100 then the value is outputted as...
5
by: max | last post by:
Dear all, I did the following analysis to conclude that the following pointer types are not compatible. Please let me know If my analysis and interpretation of the C standard are correct: ...
12
by: Frederick Gotham | last post by:
Over on comp.lang.c, Hallvard B Furuseth devised a compile-time constant representing the amount of value representation bits in an unsigned integer type. In true C fashion, here it is as a macro:...
3
by: dstevel | last post by:
The signature for strtol is: strtol( const char*, char**, int) So.. if we start with a passed "const char*" (pointer to const char), then we can't create a non-const char pointer pointer to...
4
by: Kyku | last post by:
Hello all, I'm in a process of writing a small Linux C++ program for discovering repeaded files over a filesystem. One part of this task is traversing a directory structure. This is done by the...
45
by: Zytan | last post by:
This returns the following error: "Cannot modify the return value of 'System.Collections.Generic.List<MyStruct>.this' because it is not a variable" and I have no idea why! Do lists return copies...
14
by: Tim H | last post by:
I understand the semantics of why this works the way it does. But I wonder if there's a reason for the behaviore at the line marked "QUESTION". I figured if there is an answer, someone here knows...
14
by: Jim Langston | last post by:
The output of the following program is: 1.#INF 1 But: 1.#INF 1.#INF was expected and desired. How can I read a value of infinity from a stream?
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
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...
0
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.