help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: function call misinterpreted as a variable


From: skaller
Subject: Re: function call misinterpreted as a variable
Date: Wed, 05 Oct 2005 06:14:17 +1000
User-agent: Pan/0.14.2.91 (As She Crawled Across the Table (Debian GNU/Linux))

On Mon, 03 Oct 2005 10:39:35 +0000, Zara wrote:

> 
> So, I have looked at the conflicting part:
> 
> <stl_function.h>
> 
>    template <class _Tp>
>    struct plus : public binary_function<_Tp,_Tp,_Tp> {
>      _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x + 
> __y; }
>    };
> 
> So there is the problem: The compiler is unable to decide if we want:
> 
>     double plus(double,double,double)
> 
> or
> 
>    double plus<double>::operator()(double,double,double)
> 
> In this case, it is not trying to decide between overloaded functions, 
> but between two pretty different ways to interpret the statement. And we 
> may suppose Comeau is right, as usual.

IMHO:

What is actually happening is that the gcc compiler is seeing
the 'plus' and assuming it is a class name, ignoring the function.
Then it is looking for an operator conversion:

        plus::operator double()const

The operator() is entirely irrelevant: in fact the signature would be

        double plus<double>::operator()const(double, double);

note: TWO arguments not three. In fact, if it we were just overloading
the relevant signatures would be

        plus() // generator default ctor
        plus(plus const&) // generator copy stor
        plus(double,double) // operator()
        plus(double,double,double) // user defined function

and all three are entirely disjoint, and can be overloaded
without any possible ambiguity.

So it has nothing at all to do with overloading, rather,
the compiler has to choose whether plus is a class name or
a function name, it is choosing a class for an unknown reason
BEFORE bothering to find the constructor. It is trying
in vain to find an operator conversion. It finds instead

        double plus(double,double,double);

which it should NOT find (I mean it shouldn't even see this
function) and thinks it is supposed to be a member of class plus, 
but declared in the wrong scope (should have been in std along with 
the class plus).

This is a bug in gcc, no question about it (FYI: I'm using 4.0 on Ubuntu)
at this point it is trying to do overload resolution on

        operator double()
        double(plus)

[it can find a constructor for class double with argument plus
too, except that double is a built in type ..]

double f(double);
struct f{};

int main() {
  struct f x;
  x = f(1.0);
}


abc.cpp:6: error: no match for ‘operator=’ in ‘x = f(1.0e+0)’
abc.cpp:2: note: candidates are: f& f::operator=(const f&)

Shows clearly that when a class and function are declared
in the same scope, the class takes precedence. And here:

struct f{};
double f(double);

int main() {
  struct f x = f(1.0);
}

abc.cpp:5: error: conversion from ‘double’ to non-scalar type ‘f’ requested

it is clear again, the function f is just ignored.
These messages are both sensible (whether or not the algorithm
is correct). This message:

753075304.cpp:29: error: 'plus' was not declared in this scope

is plain garbage. Comeau's message is more sensible:

"ComeauTest.c", line 27: error: "plus" is ambiguous

meaning, it can't decide if plus is a function or class.
Note this isn't an overload ambiguity, but a kinding
ambiguity (is it a typename or a function name?)


-- 
John Skaller <skaller at users dot sf dot net>
Try Felix, the successor to C++ http://felix.sf.net




reply via email to

[Prev in Thread] Current Thread [Next in Thread]