[C++-sig] Re: boost::optional from python

Neal D. Becker ndbecker2 at verizon.net
Fri Sep 10 19:56:04 CEST 2004


I guess this works:

template<typename T>
inline boost::optional<T> to_opt (object const& o) {
  if (o == object())
    return boost::optional<T>();
  else
    return boost::optional<T> (extract<T> (o)());
}


// This is the client function that wants optional arg
int G (boost::optional<int>const& o) {
  return (o ? o.get() : -1);
}

// Need this wrapper
int F (object const& o) {
  return G (to_opt<int> (o));
}

BOOST_PYTHON_MODULE(Testo)
{
  def ("F", &F, (arg ("i")=object()));

}





More information about the Cplusplus-sig mailing list