diff --git a/include/pybind11/stl.h b/include/pybind11/stl.h index 1ee2cc4b3..68b347801 100644 --- a/include/pybind11/stl.h +++ b/include/pybind11/stl.h @@ -15,6 +15,7 @@ #include #include #include +#include #if defined(_MSC_VER) #pragma warning(push) @@ -91,26 +92,31 @@ template struct map_caster { PYBIND11_TYPE_CASTER(type, _("dict<") + key_conv::name() + _(", ") + value_conv::name() + _(">")); }; -template struct type_caster> { - typedef std::vector vector_type; - typedef type_caster value_conv; +template struct list_caster { + typedef Type type; + typedef type_caster value_conv; bool load(handle src, bool convert) { list l(src, true); if (!l.check()) return false; - value.reserve(l.size()); - value.clear(); value_conv conv; + value.clear(); + reserve_maybe(l, &value); for (auto it : l) { if (!conv.load(it, convert)) return false; - value.push_back((Type) conv); + value.push_back((Value) conv); } return true; } - static handle cast(const vector_type &src, return_value_policy policy, handle parent) { + template ().reserve(0)), void>::value, int>::type = 0> + void reserve_maybe(list l, Type *) { value.reserve(l.size()); } + void reserve_maybe(list, void *) { } + + static handle cast(const Type &src, return_value_policy policy, handle parent) { list l(src.size()); size_t index = 0; for (auto const &value: src) { @@ -121,9 +127,16 @@ template struct type_caster")); + + PYBIND11_TYPE_CASTER(Type, _("list<") + value_conv::name() + _(">")); }; +template struct type_caster> + : list_caster, Type> { }; + +template struct type_caster> + : list_caster, Type> { }; + template struct type_caster> { typedef std::array array_type; typedef type_caster value_conv;