[C++-sig] returning list of class pointers that compare equal

Gary Oberbrunner garyo at genarts.com
Thu Feb 6 19:53:19 CET 2014


I have a function returning a bp::list, so:

static bp::list EXTget_params(Container *g)
{
  bp::list param_list;
  int n_params = g->n_params;
  for (int i = 0; i < n_params; i++) {
    // Need this boost::ref to put a ref to the param into the list
    // rather than a param itself.  (That would cause it to try to copy the
    // param, but it's listed as noncopyable so it would just get a TypeError.)
    param_list.append(bp::ref(g->params[i]));
  }
  return param_list;
}

Note that the Param is noncopyable.  Its lifetime is managed by Container.  The above works, except each time it runs it makes new bp::ref objects, which don't compare equal to each other:

 param1 = g.get_params()[0]
 param2 = g.get_params()[0] // should be same as param1
 if param1 == param2:
    print "Success!"

It doesn't print success, because the bp::ref objects are different, and don't have operator== to compare their referents (at least that's my guess about what's going on).  Is there something else I can use instead of bp::ref here that will cause the list items to "point to" the same param each time I call get_params()?

Thanks;

-- Gary Oberbrunner


More information about the Cplusplus-sig mailing list