Adds type_caster support for std::deque. (#1609)
* Adds std::deque to the types supported by list_caster in stl.h.
* Adds a new test_deque test in test_stl.{py,cpp}.
* Updates the documentation to include std::deque as a default
supported type.
diff --git a/docs/advanced/cast/stl.rst b/docs/advanced/cast/stl.rst
index 468bd2c..e48409f 100644
--- a/docs/advanced/cast/stl.rst
+++ b/docs/advanced/cast/stl.rst
@@ -5,7 +5,7 @@
====================
When including the additional header file :file:`pybind11/stl.h`, conversions
-between ``std::vector<>``/``std::list<>``/``std::array<>``,
+between ``std::vector<>``/``std::deque<>``/``std::list<>``/``std::array<>``,
``std::set<>``/``std::unordered_set<>``, and
``std::map<>``/``std::unordered_map<>`` and the Python ``list``, ``set`` and
``dict`` data structures are automatically enabled. The types ``std::pair<>``
diff --git a/include/pybind11/stl.h b/include/pybind11/stl.h
index 2d29a96..32f8d29 100644
--- a/include/pybind11/stl.h
+++ b/include/pybind11/stl.h
@@ -16,6 +16,7 @@
#include <unordered_map>
#include <iostream>
#include <list>
+#include <deque>
#include <valarray>
#if defined(_MSC_VER)
@@ -185,6 +186,9 @@
template <typename Type, typename Alloc> struct type_caster<std::vector<Type, Alloc>>
: list_caster<std::vector<Type, Alloc>, Type> { };
+template <typename Type, typename Alloc> struct type_caster<std::deque<Type, Alloc>>
+ : list_caster<std::deque<Type, Alloc>, Type> { };
+
template <typename Type, typename Alloc> struct type_caster<std::list<Type, Alloc>>
: list_caster<std::list<Type, Alloc>, Type> { };
diff --git a/tests/test_stl.cpp b/tests/test_stl.cpp
index 3736885..207c9fb 100644
--- a/tests/test_stl.cpp
+++ b/tests/test_stl.cpp
@@ -63,6 +63,10 @@
static std::vector<RValueCaster> lvv{2};
m.def("cast_ptr_vector", []() { return &lvv; });
+ // test_deque
+ m.def("cast_deque", []() { return std::deque<int>{1}; });
+ m.def("load_deque", [](const std::deque<int> &v) { return v.at(0) == 1 && v.at(1) == 2; });
+
// test_array
m.def("cast_array", []() { return std::array<int, 2> {{1 , 2}}; });
m.def("load_array", [](const std::array<int, 2> &a) { return a[0] == 1 && a[1] == 2; });
diff --git a/tests/test_stl.py b/tests/test_stl.py
index ba71ca3..bf185d5 100644
--- a/tests/test_stl.py
+++ b/tests/test_stl.py
@@ -23,6 +23,15 @@
assert m.cast_ptr_vector() == ["lvalue", "lvalue"]
+def test_deque(doc):
+ """std::deque <-> list"""
+ lst = m.cast_deque()
+ assert lst == [1]
+ lst.append(2)
+ assert m.load_deque(lst)
+ assert m.load_deque(tuple(lst))
+
+
def test_array(doc):
"""std::array <-> list"""
lst = m.cast_array()