Skip to content

Fix compilation error with std::nullptr_t caster #840

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions include/pybind11/cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,11 @@ struct type_caster<T, enable_if_t<std::is_arithmetic<T>::value && !is_std_char_t

template<typename T> struct void_caster {
public:
bool load(handle, bool) { return false; }
bool load(handle src, bool) {
if (src && src.is_none())
return true;
return false;
}
static handle cast(T, return_value_policy /* policy */, handle /* parent */) {
return none().inc_ref();
}
Expand Down Expand Up @@ -653,7 +657,7 @@ template <> class type_caster<void> : public type_caster<void_type> {
void *value = nullptr;
};

template <> class type_caster<std::nullptr_t> : public type_caster<void_type> { };
template <> class type_caster<std::nullptr_t> : public void_caster<std::nullptr_t> { };

template <> class type_caster<bool> {
public:
Expand Down
6 changes: 5 additions & 1 deletion tests/test_python_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,10 @@ test_initializer python_types([](py::module &m) {
const char *operator()(int) { return "int"; }
const char *operator()(std::string) { return "std::string"; }
const char *operator()(double) { return "double"; }
const char *operator()(std::nullptr_t) { return "std::nullptr_t"; }
};

m.def("load_variant", [](std::variant<int, std::string, double> v) {
m.def("load_variant", [](std::variant<int, std::string, double, std::nullptr_t> v) {
return std::visit(visitor(), v);
});

Expand Down Expand Up @@ -516,6 +517,9 @@ test_initializer python_types([](py::module &m) {
});
}
);

m.def("load_nullptr_t", [](std::nullptr_t) {}); // not useful, but it should still compile
m.def("cast_nullptr_t", []() { return std::nullptr_t{}; });
});

#if defined(_MSC_VER)
Expand Down
10 changes: 9 additions & 1 deletion tests/test_python_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,9 +378,10 @@ def test_variant(doc):
assert load_variant(1) == "int"
assert load_variant("1") == "std::string"
assert load_variant(1.0) == "double"
assert load_variant(None) == "std::nullptr_t"
assert cast_variant() == (5, "Hello")

assert doc(load_variant) == "load_variant(arg0: Union[int, str, float]) -> str"
assert doc(load_variant) == "load_variant(arg0: Union[int, str, float, None]) -> str"


def test_constructors():
Expand Down Expand Up @@ -568,3 +569,10 @@ def test_capsule_with_destructor(capture):
creating capsule
destructing capsule: 1234
"""


def test_void_caster():
import pybind11_tests as m

assert m.load_nullptr_t(None) is None
assert m.cast_nullptr_t() is None