[C++-sig] How to use make_default with keyword expression

Johan Råde johan.rade at gmail.com
Sun Feb 19 10:39:18 CET 2012


On 2/19/2012 9:46 AM, Johan Råde wrote:
> The following code works fine:
>
> using namespace boost::python;
>
> class X {};
>
> X* make_X(int n, float f) { return new X; }
>
> BOOST_PYTHON_MODULE(Test)
> {
> class_<X>("X")
> .def(
> "__init__",
> make_constructor(&make_X)
> //,(arg("n"), arg("f") = 1.0f)
> );
> }
>
> But if I uncomment the commented line, then the code does not compile.
> How do I fix that?

I figured out how to do it:

     using namespace boost::python;

     class X {};

     X* make_X(int n, float f) { return new X; }

     BOOST_PYTHON_MODULE(X)
     {
         class_<X>("X")
         .def(
             "__init__",
             make_constructor(
                 &make_X,
                 default_call_policies(),
                 (arg("n"), arg("f") = 1.0f)
             )
         );
     }



More information about the Cplusplus-sig mailing list