Quantcast

[C++-sig] gcc and explicit method types

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[C++-sig] gcc and explicit method types

Gregory Price
Thanks for this library.  I've been exposing
some of my company's C++ code base to Python,
controlling it from there, and it's already got
some people excited about what we'll be able to do.
As I port the wrapper from MSVC to GCC,
I'm having some unexpected trouble
that I hope you can help me with.


I'm compiling the following module:

---- begin foo.cpp ----
#include <boost/python.hpp>

using namespace boost::python;

class A {
public:
  A();
  int f();
};

BOOST_PYTHON_MODULE(foo)
{
  class_<A>("A")
    .def("f", A::f);
}
---- end foo.cpp ----

Using Visual Studio (with whatever enormous inscrutable
command line is produced by my project settings)[1],
this compiles and works fine.

Using GCC produces the following error:

---- begin transcript ----
$ g++ -I/usr/include/python2.4  -c foo.cpp
foo.cpp: In function `void init_module_foo()':
foo.cpp:15: no matching function for call to `boost::python::class_<A,
   boost::python::detail::not_specified,
boost::python::detail::not_specified,
   boost::python::detail::not_specified>::def(const char[2], <unknown
type>)'
/usr/include/boost/python/class.hpp:234: candidates are:
   boost::python::class_<T, X1, X2, X3>& boost::python::class_<T, X1,
X2,
   X3>::def(const char*, F) [with F = int (A::*)(), W = A, X1 =
   boost::python::detail::not_specified, X2 =
   boost::python::detail::not_specified, X3 =
   boost::python::detail::not_specified]
---- end transcript ----

I observe in particular the `unknown type' bit.
I can fix that with an explicit cast:

  class_<A>("A")
    .def("f", (int (A::*)())A::f);

getting a less imposing message:

---- begin transcript ----
$ g++ -I/usr/include/python2.4  -c foo.cpp
foo.cpp: In function `void init_module_foo()':
foo.cpp:15: assuming pointer to member `int A::f()'
foo.cpp:15: (a pointer to member can only be formed with `&A::f()')
---- end transcript ----

which can be fixed as suggested with

  class_<A>("A")
    .def("f", (int (A::*)())&A::f);

for no message at all.



I could apply this fix to all the methods in my module.
But I believe the Boost.Python magic is supposed to
make these explicit type annotations unnecessary.
What should I be doing differently?


(I'm using Boost 1.32; but I checked Boost.Python's news file
  http://boost.org/libs/python/doc/news.html
and don't see anything pertinent since then.)


Thanks,
Greg



---- begin transcript ----
$ g++ --version | head -1
g++ (GCC) 3.2.3 20030502 (Red Hat Linux 3.2.3-42)

$ grep '#define BOOST_VERSION ' `locate boost/version.hpp`
#define BOOST_VERSION 103200
---- end transcript ----


[1] For completeness' sake, the command-line options follow:
/Od /Ob2 /I "..\." /I "c:\python24\include" /I
"c:\tools\boost_1_32_0\src"
/D "WIN32" /D "_WINDOWS" /D "_DEBUG" /D "_USRDLL" /D "_WINDLL" /FD /EHsc

/RTC1 /MDd /GS /Zc:forScope /GR /Fo".\Debug/" /Fd".\Debug/vc70.pdb"
/FR".\Debug/" /W3 /nologo /c /Zi /TP



=============================================================================================
Email transmissions can not be guaranteed to be secure or error-free, as information
could be intercepted, corrupted, lost, destroyed, arrive late or incomplete, or contain
viruses.  The sender therefore does not accept liability for any errors or omissions in
the contents of this message which arise as a result of email transmission.  In addition,
the information contained in this email message is intended only for use of the
individual or entity named above.  If the reader of this message is not the intended
recipient, or the employee or agent responsible to deliver it to the intended recipient,
you are hereby notified that any dissemination, distribution, or copying of this communication,
disclosure of the parties to it, or any action taken or omitted to be taken in reliance on it,
is strictly prohibited, and may be unlawful.  If you are not the intended recipient please
delete this email message.
==============================================================================================

_______________________________________________
C++-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/c++-sig
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [C++-sig] gcc and explicit method types

Stefan Seefeld
Gregory Price wrote:

> Thanks for this library.  I've been exposing
> some of my company's C++ code base to Python,
> controlling it from there, and it's already got
> some people excited about what we'll be able to do.
> As I port the wrapper from MSVC to GCC,
> I'm having some unexpected trouble
> that I hope you can help me with.
>
>
> I'm compiling the following module:
>
> ---- begin foo.cpp ----
> #include <boost/python.hpp>
>
> using namespace boost::python;
>
> class A {
> public:
>   A();
>   int f();
> };
>
> BOOST_PYTHON_MODULE(foo)
> {
>   class_<A>("A")
>     .def("f", A::f);

_______________^

that should be '&A::f'

> }
> ---- end foo.cpp ----
>
> Using Visual Studio (with whatever enormous inscrutable
> command line is produced by my project settings)[1],
> this compiles and works fine.

Yeah, I noticed that MSVC accepted the above, even though
the correct syntax requires the address-of operator.

Regards,
                Stefan
_______________________________________________
C++-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/c++-sig
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [C++-sig] gcc and explicit method types

Yves Secretan
In reply to this post by Gregory Price
BOOST_PYTHON_MODULE(foo)
{
  class_<A>("A")
    .def("f", &A::f);
             ^^^^^^^ Should do the job
}


_______________________________________________
C++-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/c++-sig
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [C++-sig] gcc and explicit method types

Gregory Price
In reply to this post by Gregory Price

Stefan Seefeld wrote (as did Yves Secretan):
> >     .def("f", A::f);
>
> _______________^
>
> that should be '&A::f'

Thanks!  Fixed.

Greg

=============================================================================================
Email transmissions can not be guaranteed to be secure or error-free, as information
could be intercepted, corrupted, lost, destroyed, arrive late or incomplete, or contain
viruses.  The sender therefore does not accept liability for any errors or omissions in
the contents of this message which arise as a result of email transmission.  In addition,
the information contained in this email message is intended only for use of the
individual or entity named above.  If the reader of this message is not the intended
recipient, or the employee or agent responsible to deliver it to the intended recipient,
you are hereby notified that any dissemination, distribution, or copying of this communication,
disclosure of the parties to it, or any action taken or omitted to be taken in reliance on it,
is strictly prohibited, and may be unlawful.  If you are not the intended recipient please
delete this email message.
==============================================================================================

_______________________________________________
C++-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/c++-sig
Loading...