Skip to content

Commit

Permalink
Remove implicit conversion operators on sexp
Browse files Browse the repository at this point in the history
  • Loading branch information
DavisVaughan committed Aug 22, 2024
1 parent 6a2bc9b commit f499977
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
21 changes: 10 additions & 11 deletions cpp11test/src/test-function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@
context("function-C++") {
test_that("functions can be called") {
auto median = cpp11::package("stats")["median"];
double res = median(cpp11::as_sexp({1., 2., 3., NA_REAL}), true);
expect_true(res == 2.);
cpp11::sexp res = median(cpp11::as_sexp({1., 2., 3., NA_REAL}), true);
expect_true(cpp11::as_cpp<double>(res) == 2.);

double res2 = median(cpp11::as_sexp({1., 2., 3., NA_REAL}), false);
expect_true(ISNAN(res2));
cpp11::sexp res2 = median(cpp11::as_sexp({1., 2., 3., NA_REAL}), false);
expect_true(ISNAN(cpp11::as_cpp<double>(res2)));
}

test_that("functions can be called with named arguments") {
using namespace cpp11::literals;

auto median = cpp11::package("stats")["median"];
double res = median("x"_nm = {1., 2., 3., NA_REAL}, "na.rm"_nm = true);
expect_true(res == 2.);
cpp11::sexp res = median("x"_nm = {1., 2., 3., NA_REAL}, "na.rm"_nm = true);
expect_true(cpp11::as_cpp<double>(res) == 2.);

double res2 = median(cpp11::as_sexp({1., 2., 3., NA_REAL}), false);
expect_true(ISNAN(res2));
cpp11::sexp res2 = median(cpp11::as_sexp({1., 2., 3., NA_REAL}), false);
expect_true(ISNAN(cpp11::as_cpp<double>(res2)));
}

test_that("base functions can be called") {
Expand All @@ -29,10 +29,9 @@ context("function-C++") {
auto close = cpp11::package("base")["close"];

cpp11::sexp con = file("foo");
cpp11::sexp res = isOpen(con);

bool res = isOpen(con);

expect_true(res == false);
expect_true(cpp11::as_cpp<bool>(res) == false);

close(con);
}
Expand Down
4 changes: 1 addition & 3 deletions inst/include/cpp11/sexp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ class sexp {
}

operator SEXP() const { return data_; }
operator double() const { return REAL_ELT(data_, 0); }
operator size_t() const { return REAL_ELT(data_, 0); }
operator bool() const { return LOGICAL_ELT(data_, 0); }

SEXP data() const { return data_; }
};

Expand Down

0 comments on commit f499977

Please sign in to comment.