Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove implicit conversion operators on sexp #390

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# cpp11 (development version)

* Removed implicit conversion operators from `sexp` to `double`, `bool`, and
`size_t`. Please use `cpp11::as_cpp()` for this instead, which is much safer
(#390).

* `cpp11::writable::r_vector<T>::iterator` no longer implicitly deletes its
copy assignment operator (#360).

Expand Down
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
Loading