Skip to content

Commit c91b349

Browse files
authored
Enhance Rcpp::Nullable::as with explicit cast (Closes #1470) (#1471)
* Enhance Rcpp::Nullable::as with explicit cast (Closes #1470) * Add operator T * Conditrion nullable<String> test on R 4.3.0 or later * Move the conditional to operator T() * Hide Nullable::operatorT() behind a #define This passes reverse-dependency checks
1 parent e27a256 commit c91b349

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

ChangeLog

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
1+
2026-04-12 Dirk Eddelbuettel <edd@debian.org>
2+
3+
* inst/include/Rcpp/Nullable.h (Nullable): 'operator T()' is now
4+
behind an opt-in #define
5+
16
2026-04-09 Dirk Eddelbuettel <edd@debian.org>
27

38
* DESCRIPTION (Version, Date): Roll micro version and date
9+
* inst/include/Rcpp/Nullable.h (T): Also add 'operator T()'
10+
(conditional on R (>= 4.3.0) as older versions croak)
11+
12+
2026-04-08 Dirk Eddelbuettel <edd@debian.org>
13+
14+
* inst/include/Rcpp/Nullable.h (as): Enhance by adding an explicit
15+
Rcpp::as<T>() aiding cases where implicit as<> does not suffice
416

517
2026-04-07 Dirk Eddelbuettel <edd@debian.org>
618

inst/include/Rcpp/Nullable.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,18 @@ namespace Rcpp {
8181
return m_sexp;
8282
}
8383

84+
#if R_VERSION > R_Version(4,3,0) && defined(RCPP_ENABLE_NULLABLE_T)
85+
/**
86+
* operator T() to return nullable object
87+
*
88+
* @throw 'not initialized' if object has not been set
89+
*/
90+
inline operator T() const {
91+
checkIfSet();
92+
return Rcpp::as<T>(m_sexp);
93+
}
94+
#endif
95+
8496
/**
8597
* get() accessor for object
8698
*
@@ -126,7 +138,7 @@ namespace Rcpp {
126138
/**
127139
* Returns m_sexp as a T
128140
*/
129-
inline T as() { return get(); }
141+
inline T as() { return Rcpp::as<T>(get()); }
130142

131143
/**
132144
* Return a clone of m_sexp as a T

inst/tinytest/test_misc.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ expect_equal( testNullableIsUsable(M), M)
168168
# test.NullableIsUsableFalse <- function() {
169169
expect_true(is.null(testNullableIsUsable(NULL)))
170170

171-
# test.NullableString <- function() {
171+
## test.NullableString <- function() {
172172
expect_equal(testNullableString(), "")
173173
expect_equal(testNullableString("blah"), "blah")
174174

0 commit comments

Comments
 (0)