operator double() surprise in cxx

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Hunter

    operator double() surprise in cxx


    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.c pp: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

  • Benoît Dejean

    #2
    Re: operator double() surprise in cxx

    Le Thu, 29 Apr 2004 21:35:51 -0500, John Hunter a écrit :
    [color=blue]
    >
    > 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]) );[/color]

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

    Comment

    • Isaac To

      #3
      Re: operator double() surprise in cxx

      >>>>> "Benoît" == Benoît Dejean <bnetNOSPAM@ifr ance.com> writes:

      Benoît> Le Thu, 29 Apr 2004 21:35:51 -0500, John Hunter a écrit :[color=blue][color=green]
      >> 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]) );[/color][/color]

      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.

      Comment

      • Jeremy Yallop

        #4
        Re: operator double() surprise in cxx

        Isaac To wrote:[color=blue][color=green][color=darkred]
        >>>>>> "Benoît" == Benoît Dejean <bnetNOSPAM@ifr ance.com> writes:[/color][/color]
        >
        > Benoît> Le Thu, 29 Apr 2004 21:35:51 -0500, John Hunter a écrit :[color=green][color=darkred]
        > >> 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]) );[/color][/color]
        >
        > Benoît> everything that looks/tastes/sounds like a function declaration
        > Benoît> is (even with parameters.
        >
        > Is it really a bug in g++?[/color]

        No.
        [color=blue]
        > 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.[/color]

        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.

        Comment

        • Isaac To

          #5
          Re: operator double() surprise in cxx

          >>>>> "Jeremy" == Jeremy Yallop <jeremy@jdyallo p.freeserve.co. uk> writes:

          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.

          Comment

          • John Hunter

            #6
            Re: operator double() surprise in cxx

            >>>>> "Benoît" == Benoît Dejean <bnetNOSPAM@ifr ance.com> writes:[color=blue][color=green]
            >> double l( Py::Float(rect[0]) ); double b( Py::Float(rect[1]) );[/color][/color]

            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

            Comment

            • Christophe Cavalaria

              #7
              Re: operator double() surprise in cxx

              John Hunter wrote:[color=blue][color=green][color=darkred]
              >>>>>>"Benoît " == Benoît Dejean <bnetNOSPAM@ifr ance.com> writes:[/color][/color]
              >[color=green][color=darkred]
              > >> double l( Py::Float(rect[0]) ); double b( Py::Float(rect[1]) );[/color][/color]
              >
              > 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?[/color]

              Exactly
              [color=blue]
              > Hmm. This line was inside a
              > class method -- I didn't think you could declare functions in a class
              > method....[/color]

              Well, you can declare a function nearly everywhere in fact.
              [color=blue]
              > Still confused, but perhaps on the road to enlightenment.[/color]

              Try that :

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

              Comment

              • Benoît Dejean

                #8
                Re: operator double() surprise in cxx

                Le Fri, 30 Apr 2004 15:50:50 +0200, Christophe Cavalaria a écrit :
                [color=blue]
                > John Hunter wrote:[color=green][color=darkred]
                >>>>>>>"Benoî t" == Benoît Dejean <bnetNOSPAM@ifr ance.com> writes:[/color][/color][/color]
                [color=blue][color=green]
                >> Benoît> everything that looks/tastes/sounds like a function
                >> Benoît> declaration is (even with parameters.[/color][/color]
                [color=blue]
                > Exactly[/color]

                BS « faced » this problem in a pre-Kona paper



                this mostly because of the C-style where you declared function nearly
                everywhere....

                Comment

                Working...