On Fri, Jul 24, 2015 at 9:51 AM, slonik <
slon...@gmail.com> wrote:
> Hi All,
>
> Quite a few classes in the C++ standard library (std::uniq_ptr,
> std::shared_ptr, std::function, io-streams, etc) define "operator bool()" so
> that their objects can be used in a boolean context (e.g. conditionals):
>
> #include <memory>
> std::unique_ptr<int> uptr;
> if(uptr) {do something if uptr contains non-null ptr}
>
> I am curious how to use such C++ objects in Cython conditionals ??
> To the best of my knowledge Cython does not support "operator bool()" for
> C++ types.
> Instead, there is support for the C++ "operator!()".
Currently "not not" is the best approach. However,
https://github.com/cython/cython/pull/421 may do what you want.
> In Cython I can do the following:
>
> cdef extern from "<memory>" namespace "std" nogil:
> cdef cppclass unique_ptr[T]:
> unique_ptr()
> bint operator!()
> # remaining declarations ...
>
> Now I can use conditionals together with the 'not' logical operation:
>
> if not uptr: # do something if uptr is false
>
> Unfortunately, a simple Cython construction " if uptr: "
> results in Cython compile-time error: "Type 'unique_ptr[int]' not acceptable
> as a boolean"
>
> An ugly work around would be to use double "not":
>
> if not not uptr: # this works but is ugly!
>
> Does anyone know a cleaner way to use C++ objects in a boolean context in
> Cython ??
>
> To keep the discussion focused I would like to reiterate that the question
> is about C++ objects and *NOT* python extension types. Suggestions to use
> "def __bool__(self)" or "def __nonrezo__()" would not work in the cython's
> C++ context.
>
> Thanks,
> --Leo
>
> --
>
> ---
> You received this message because you are subscribed to the Google Groups
> "cython-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to
cython-users...@googlegroups.com.
> For more options, visit
https://groups.google.com/d/optout.