Topic: Does this ambiguity exist?


Author: mbenkmann@gmx.de (Matthias Benkmann)
Date: Thu, 16 Aug 2001 16:28:21 GMT
Raw View
After reading the thread about binding temporaries to non-const
references on comp.lang.c++.moderated I tried the following with my
Borland C++ 5.5.

#include <iostream>

using namespace std;

void f(long& l)
{
 l=10;
};

void f(long l)
{
  cout<<l;
};

int main()
{
  int i=10;
  f(i);
  cout<<i;
  return 0;
};

The compiler complains about an ambiguity between the 2 functions. But
because the temporary long created as a result of calling f() with an
integer argument can not be bound to a long&, it seems like the only
function that should be candidate is void f(long). Is there something
I am missing or is the compiler defective?

MSB

----
By the way:
Vacuum cleaners suck!

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Thu, 16 Aug 2001 19:21:38 GMT
Raw View
In article <3b7bc2bd.5162171@news.cis.dfn.de>, Matthias Benkmann
<mbenkmann@gmx.de> writes
>The compiler complains about an ambiguity between the 2 functions. But
>because the temporary long created as a result of calling f() with an
>integer argument can not be bound to a long&, it seems like the only
>function that should be candidate is void f(long). Is there something
>I am missing or is the compiler defective?

Yes, the compiler is not allowed to make that distinction. No program
can have an overloaded set of functions in which the only distinction is
whether something is passed by value or reference. This is only one of
several cases where the compiler is not required to (allowed to) make
inferences that may seem clear to the programmer.


Francis Glassborow      ACCU
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Nicola Musatti <objectway@divalsim.it>
Date: Wed, 22 Aug 2001 00:26:12 GMT
Raw View

Matthias Benkmann wrote:
[...]
> The compiler complains about an ambiguity between the 2 functions. But
> because the temporary long created as a result of calling f() with an
> integer argument can not be bound to a long&, it seems like the only
> function that should be candidate is void f(long). Is there something
> I am missing or is the compiler defective?

Unfortunately the Borland compiler only emits a warning when a temporary
is bound to a non const reference. While this behaviour is standard
compliant, it is certainly a quality of implementation (or lack thereof)
issue.

Cheers,
Nicola Musatti

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: comeau@panix.com (Greg Comeau)
Date: Thu, 23 Aug 2001 02:29:53 GMT
Raw View
In article <3B821D26.9C497DFC@divalsim.it>,
Nicola Musatti  <Nicola.Musatti@ObjectWay.it> wrote:
>Unfortunately the Borland compiler only emits a warning when a temporary
>is bound to a non const reference. While this behaviour is standard
>compliant,

You mean because although it is not an error, at least it is a
diagnostic, and so therefore following the standard?

>it is certainly a quality of implementation (or lack thereof)
>issue.

What is?  Whether the diagnostic is a warning or an error?

(I'm not disagreeing with any of this (yet:}), just trying to
understand what you mean.)
--
Greg Comeau                 Countdown to "export": December 1, 2001
Comeau C/C++ ONLINE ==>     http://www.comeaucomputing.com/tryitout
World Class Compilers:  Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Nicola Musatti <objectway@divalsim.it>
Date: Thu, 23 Aug 2001 17:58:57 GMT
Raw View

Greg Comeau wrote:
>
> In article <3B821D26.9C497DFC@divalsim.it>,
> Nicola Musatti  <Nicola.Musatti@ObjectWay.it> wrote:
> >Unfortunately the Borland compiler only emits a warning when a temporary
> >is bound to a non const reference. While this behaviour is standard
> >compliant,
>
> You mean because although it is not an error, at least it is a
> diagnostic, and so therefore following the standard?

Exactly.

> >it is certainly a quality of implementation (or lack thereof)
> >issue.
>
> What is?  Whether the diagnostic is a warning or an error?

In my opinion binding of temporaries to non const references leads to
subtle errors and is disallowed in the standard with reason. A very
simple example:

#include <iostream>

void f(double & v) {
  v = 0.0;
}

int main() {
  int i = 2;
  f(i);
  std::cout << i << std::endl;
}

I don't think that a warning is enough to help detect these problems,
while it only takes a local variable to workaround those situations in
which you really want to pass a temporary to a function that takes a non
const reference.

In sum I wish Borland's compiler aborted the compilation instead of
letting you write subtly non compliant code.

> (I'm not disagreeing with any of this (yet:}), just trying to
> understand what you mean.)

Hope I've been clearer now.

Cheers,
Nicola Musatti

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]