[C++-sig] Overriding operator == and = for None

Ralf W. Grosse-Kunstleve rwgk at yahoo.com
Wed Jun 21 22:49:32 CEST 2006


I think you can do what you want based on boost::optional. You may want to look
here for an example:

  http://phenix-online.org/cctbx_sources/boost_adaptbx/
    optional_conversions.h
    optional_ext.cpp
    tst_optional.py

Note that "x is None" works correctly, not just "x == None".

The boost::optional documentation is here:

  http://www.boost.org/libs/optional/doc/optional.html

Feel free to copy optional_conversions.h if you are interested.

--- Will Lee <lee.will at gmail.com> wrote:

> Is there a way I can override the operator == and = for a wrapped c++ object
> to take in a Python None type?  Essentially, I'd like to define a class
> where it can do:
> 
> #NullableInt is a struct that holds an int and a boolean, indicating whether
> it is valid or not
> c = NullableInt(3)
> # NullableInt wraps a C++ std::vector<NullableInt>
> cv = NullableIntVec()
> cv.append(c) # ok
> cv[0] = None
> print cv[0]
> # should print out "None", and the C++'s struct would hold a boolean that's
> set to false
> 
> # Generates an invalid Int
> cInvalid = NullableInt()
> # Should be true, it won't remove object but setting the underlying c++
> object to None)
> cv[0] == cInvalid
> # Should also be true if we can override ==
> cv[0] == None
> 
> Does anybody know if it is posslbe in Boost.Python to have this behavior?
> 
> Will


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 



More information about the Cplusplus-sig mailing list