diff --git a/NEWS.md b/NEWS.md index 6ae72571..4224459d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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::iterator` no longer implicitly deletes its copy assignment operator (#360). diff --git a/cpp11test/src/test-function.cpp b/cpp11test/src/test-function.cpp index 678a5ee2..22691a8c 100644 --- a/cpp11test/src/test-function.cpp +++ b/cpp11test/src/test-function.cpp @@ -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(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(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(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(res2))); } test_that("base functions can be called") { @@ -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(res) == false); close(con); } diff --git a/inst/include/cpp11/sexp.hpp b/inst/include/cpp11/sexp.hpp index 7e9fdfea..25ced3c3 100644 --- a/inst/include/cpp11/sexp.hpp +++ b/inst/include/cpp11/sexp.hpp @@ -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_; } };