Skip to content

Commit 07b7ba7

Browse files
committed
Ensure we call operator SEXP() when copying or moving writable vectors
The previous code was unintentially avoiding this call by copying the `.data_` member directly. Fixes #91
1 parent 97cd93a commit 07b7ba7

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# cpp11 (development version)
22

3+
* `data_frame()` objects now have the number of rows correctly set as real length, not the reserved length (#91)
4+
35
# cpp11 0.2.1
46

57
* Ensures backwards compatibility with code generation from cpp11 0.1.0 (#88)

cpp11test/src/test-data_frame.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,17 @@ context("data_frame-C++") {
9090
expect_true(cpp11::integers(df["a"])[0] == 1);
9191
expect_true(cpp11::strings(df["b"])[2] == "c");
9292
}
93+
94+
test_that("growing vectors uses proper length") {
95+
using namespace cpp11::literals;
96+
97+
cpp11::writable::integers x, y;
98+
for (int i = 0; i < 10; ++i) {
99+
x.push_back(i);
100+
y.push_back(i);
101+
}
102+
cpp11::writable::data_frame out({"x"_nm = x, "y"_nm = y});
103+
104+
expect_true(out.nrow() == 10);
105+
}
93106
}

inst/include/cpp11/r_vector.hpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -760,22 +760,20 @@ inline typename r_vector<T>::iterator r_vector<T>::find(const r_string& name) co
760760

761761
template <typename T>
762762
inline r_vector<T>::r_vector(const r_vector<T>& rhs)
763-
: cpp11::r_vector<T>(safe[Rf_shallow_duplicate](rhs.data_)),
763+
: cpp11::r_vector<T>(safe[Rf_shallow_duplicate](rhs)),
764764
protect_(protect_sexp(data_)),
765765
capacity_(rhs.capacity_) {}
766766

767767
template <typename T>
768768
inline r_vector<T>::r_vector(r_vector<T>&& rhs)
769-
: cpp11::r_vector<T>(rhs.data_),
770-
protect_(protect_sexp(data_)),
771-
capacity_(rhs.capacity_) {
769+
: cpp11::r_vector<T>(rhs), protect_(protect_sexp(data_)), capacity_(rhs.capacity_) {
772770
rhs.data_ = R_NilValue;
773771
rhs.protect_ = R_NilValue;
774772
}
775773

776774
template <typename T>
777775
inline r_vector<T>::r_vector(const cpp11::r_vector<T>& rhs)
778-
: cpp11::r_vector<T>(safe[Rf_shallow_duplicate](rhs.data_)),
776+
: cpp11::r_vector<T>(safe[Rf_shallow_duplicate](rhs)),
779777
protect_(protect_sexp(data_)),
780778
capacity_(rhs.length_) {}
781779

0 commit comments

Comments
 (0)