Skip to content

Commit 17983e7

Browse files
voxmeawjakob
authored andcommitted
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.
1 parent 8f5b7fc commit 17983e7

File tree

4 files changed

+18
-1
lines changed

4 files changed

+18
-1
lines changed

docs/advanced/cast/stl.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Automatic conversion
55
====================
66

77
When including the additional header file :file:`pybind11/stl.h`, conversions
8-
between ``std::vector<>``/``std::list<>``/``std::array<>``,
8+
between ``std::vector<>``/``std::deque<>``/``std::list<>``/``std::array<>``,
99
``std::set<>``/``std::unordered_set<>``, and
1010
``std::map<>``/``std::unordered_map<>`` and the Python ``list``, ``set`` and
1111
``dict`` data structures are automatically enabled. The types ``std::pair<>``

include/pybind11/stl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <unordered_map>
1717
#include <iostream>
1818
#include <list>
19+
#include <deque>
1920
#include <valarray>
2021

2122
#if defined(_MSC_VER)
@@ -185,6 +186,9 @@ template <typename Type, typename Value> struct list_caster {
185186
template <typename Type, typename Alloc> struct type_caster<std::vector<Type, Alloc>>
186187
: list_caster<std::vector<Type, Alloc>, Type> { };
187188

189+
template <typename Type, typename Alloc> struct type_caster<std::deque<Type, Alloc>>
190+
: list_caster<std::deque<Type, Alloc>, Type> { };
191+
188192
template <typename Type, typename Alloc> struct type_caster<std::list<Type, Alloc>>
189193
: list_caster<std::list<Type, Alloc>, Type> { };
190194

tests/test_stl.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ TEST_SUBMODULE(stl, m) {
6363
static std::vector<RValueCaster> lvv{2};
6464
m.def("cast_ptr_vector", []() { return &lvv; });
6565

66+
// test_deque
67+
m.def("cast_deque", []() { return std::deque<int>{1}; });
68+
m.def("load_deque", [](const std::deque<int> &v) { return v.at(0) == 1 && v.at(1) == 2; });
69+
6670
// test_array
6771
m.def("cast_array", []() { return std::array<int, 2> {{1 , 2}}; });
6872
m.def("load_array", [](const std::array<int, 2> &a) { return a[0] == 1 && a[1] == 2; });

tests/test_stl.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ def test_vector(doc):
2323
assert m.cast_ptr_vector() == ["lvalue", "lvalue"]
2424

2525

26+
def test_deque(doc):
27+
"""std::deque <-> list"""
28+
lst = m.cast_deque()
29+
assert lst == [1]
30+
lst.append(2)
31+
assert m.load_deque(lst)
32+
assert m.load_deque(tuple(lst))
33+
34+
2635
def test_array(doc):
2736
"""std::array <-> list"""
2837
lst = m.cast_array()

0 commit comments

Comments
 (0)