operator double() surprise in cxx

J

John Hunter

I am using pycxx 5.2.2 to generate some extension code. I want to
extract some doubles from some python sequences

When I do

double l( Py::Float(rect[0]) );
double b( Py::Float(rect[1]) );

and then later call something like l+b in the extensions code, I get
the compile time error

src/_backend_agg2.cpp:110: error: invalid operands of types `double
()(Py::Float*)' and `double ()(Py::Float*)' to binary `operator+'

But

double l = Py::Float(rect[0]) ;
double b = Py::Float(rect[1]) ;

compiles fine.


I see in the module docs that Py::Float overloads operator double(),
but there is something I am not understanding. How are these 2 cases
different. In both cases I expect l and b to be of type double, so
why the difference? I guess I don't understand exactly what it mean
in C++ to overload operator double().

Thanks,
John Hunter
 
?

=?iso-8859-1?q?Beno=EEt_Dejean?=

Le Thu, 29 Apr 2004 21:35:51 -0500, John Hunter a écrit :
I am using pycxx 5.2.2 to generate some extension code. I want to
extract some doubles from some python sequences

When I do

double l( Py::Float(rect[0]) );
double b( Py::Float(rect[1]) );

everything that looks/tastes/sounds like a function declaration is (even
with parameters.
 
I

Isaac To

Benoît> Le Thu, 29 Apr 2004 21:35:51 -0500, John Hunter a écrit :
>> I am using pycxx 5.2.2 to generate some extension code. I want to
>> extract some doubles from some python sequences
>>
>> When I do
>>
>> double l( Py::Float(rect[0]) ); double b( Py::Float(rect[1]) );

Benoît> everything that looks/tastes/sounds like a function declaration
Benoît> is (even with parameters.

Is it really a bug in g++? No matter how I look at

Py::Float(rect[0])

it does not look like a type "Py::Float*" that is indicated by the error
message. Perhaps it looks like a (Py::Float (*) (rect*)) if g++ think that
rect is a type instead of a variable, but does it really know some type
called rect, and if not, why no error about unknown type on the "function
declaration"?

Regards,
Isaac.
 
J

Jeremy Yallop

Isaac said:
Benoît> Le Thu, 29 Apr 2004 21:35:51 -0500, John Hunter a écrit :
I am using pycxx 5.2.2 to generate some extension code. I want to
extract some doubles from some python sequences

When I do

double l( Py::Float(rect[0]) ); double b( Py::Float(rect[1]) );

Benoît> everything that looks/tastes/sounds like a function declaration
Benoît> is (even with parameters.

Is it really a bug in g++?
No.

No matter how I look at

Py::Float(rect[0])

it does not look like a type "Py::Float*" that is indicated by the error
message.

As a declaration, it's a zero-length array of Py::Float, with a
parenthesized declarator, i.e. the same as

Py::Float rect[0];

As a parameter declaration, it's equivalent to

Py::Float *rect;

because of the way C++ handles arrays.

Jeremy.
 
J

John Hunter

Benoît" == Benoît Dejean said:
>> double l( Py::Float(rect[0]) ); double b( Py::Float(rect[1]) );

Benoît> everything that looks/tastes/sounds like a function
Benoît> declaration is (even with parameters.

Interesting.

So you are saying I can do

double l(1);

because this could not be a function declaration so the double
constructor is called with an integer argument, but in

double l( Py::Float(rect[0]) );

the compiler thinks I am declaring a function l that takes Py::Float*
as an argument and returns a double? Hmm. This line was inside a
class method -- I didn't think you could declare functions in a class
method....

Still confused, but perhaps on the road to enlightenment.

JDH
 
I

Isaac To

Jeremy> As a declaration, it's a zero-length array of Py::Float, with a
Jeremy> parenthesized declarator, i.e. the same as

Jeremy> Py::Float rect[0];

Ah right, thanks for pointing that out.

Regards,
Isaac.
 
C

Christophe Cavalaria

John said:
double l( Py::Float(rect[0]) ); double b( Py::Float(rect[1]) );

Benoît> everything that looks/tastes/sounds like a function
Benoît> declaration is (even with parameters.

double l( Py::Float(rect[0]) );

the compiler thinks I am declaring a function l that takes Py::Float*
as an argument and returns a double?
Exactly

Hmm. This line was inside a
class method -- I didn't think you could declare functions in a class
method....

Well, you can declare a function nearly everywhere in fact.
Still confused, but perhaps on the road to enlightenment.

Try that :

double l(( Py::Float(rect[0]) ));
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top