Skip to content

Commit 72f368e

Browse files
committed
Make sure ListOf<T> converts sub-objects to T
1 parent 8d35246 commit 72f368e

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

inst/include/Rcpp/vector/ListOf.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,14 @@ class ListOf {
3131

3232
ListOf(): list(R_NilValue) {}
3333

34-
ListOf(SEXP data_): list(data_) {}
34+
ListOf(SEXP data_): list(data_) {
35+
std::transform(list.begin(), list.end(), list.begin(), as<T>);
36+
}
3537

3638
template <typename U>
37-
ListOf(const U& data_): list(data_) {}
39+
ListOf(const U& data_): list(data_) {
40+
std::transform(list.begin(), list.end(), list.begin(), as<T>);
41+
}
3842

3943
ListOf(const ListOf& other): list(other.list) {}
4044

inst/unitTests/cpp/ListOf.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,8 @@ int test_sub_calls(NVList x) {
9393
NumericVector test_nested_listof(ListOf< ListOf<NumericVector> > x) {
9494
return x[0][0];
9595
}
96+
97+
// [[Rcpp::export]]
98+
ListOf<IntegerVector> test_return_IVList(List x) {
99+
return x;
100+
}

inst/unitTests/runit.ListOf.R

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,17 @@ if (.runThisTest) {
7474
)
7575
}
7676

77+
test.ListOf.convert.implicit <- function() {
78+
checkEquals(
79+
test_return_IVList(list(1, 2, 3)),
80+
list(1L, 2L, 3L)
81+
)
82+
}
83+
84+
test.ListOf.convert.fail <- function() {
85+
checkException(
86+
test_return_IVList(list("a", "b", "c"))
87+
)
88+
}
89+
7790
}

0 commit comments

Comments
 (0)