[Q] how to use C++ object in a boolean context?

62 views
Skip to first unread message

slonik

unread,
Jul 24, 2015, 5:53:05 PM7/24/15
to cython-users
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!()".

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

Robert Bradshaw

unread,
Jul 24, 2015, 9:03:06 PM7/24/15
to cython...@googlegroups.com
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.

slonik

unread,
Jul 27, 2015, 2:42:18 AM7/27/15
to cython-users, robe...@gmail.com


On Friday, July 24, 2015 at 9:03:06 PM UTC-4, Robert Bradshaw wrote:

Currently "not not" is the best approach. However,
https://github.com/cython/cython/pull/421 may do what you want.


Robert,
a lot of thanks for implementing this feature so quickly! I cherry-picked your commit on top of the 0.22.x branch and operator bool() works like a charm!

--Leo
Reply all
Reply to author
Forward
0 new messages