[C++-sig] Default argument and return policies

Brian McCandless bmccandl at llnl.gov
Fri Sep 6 03:49:28 CEST 2002

I have a function that uses default arguments and also needs a call policy.
But I was not able to get this to work.  I last grabbed the code from
CVS yesterday.
Am I doing something wrong or is this an omission in the library?

Here is my example:

#include <boost/python/class.hpp>
#include <boost/python/return_value_policy.hpp>
#include <boost/python/manage_new_object.hpp>
#include <boost/python/module_init.hpp>
#include <boost/python/def.hpp>

using namespace boost::python;

struct Data {
  Data(double d) : val(d) {}
  double val;

Data* makeData1(double f, double d = 1.0) {
  return new Data(f * d);

Data makeData2(double f, double d = 1.0) {
  return Data(f * d);

BOOST_PYTHON_FUNCTION_GENERATOR(makeData1_stubs, makeData1, 1, 2)
BOOST_PYTHON_FUNCTION_GENERATOR(makeData2_stubs, makeData2, 1, 2)

  // works, but you have to pass in two arguments
  def("makeData1", makeData1,

  // works with default arguments as expected.
  def("makeData2", makeData2, makeData2_stubs());

#if 0
  // Does not compile!
  def("makeData1_with_defaults", makeData1, makeData1_stubs(),

  class_<Data> data("Data", args<double>());
  data.def_readwrite("val", &Data::val);

 Brian McCandless     bmccandl at llnl.gov     925/424-2690

More information about the Cplusplus-sig mailing list