@@ -155,6 +155,23 @@ context("integers-C++") {
155155 expect_true (x[1 ] == 3 );
156156 expect_true (x[2 ] == 4 );
157157 }
158+ test_that (" integers.value()" ) {
159+ cpp11::writable::integers x;
160+ x.push_back (1 );
161+ x.push_back (2 );
162+ x.push_back (3 );
163+
164+ // Test that value() returns the same as operator[] but as T directly
165+ expect_true (x.value (0 ) == 1 );
166+ expect_true (x.value (1 ) == 2 );
167+ expect_true (x.value (2 ) == 3 );
168+
169+ // Test that value() works with C-style formatting (this was the original issue in
170+ // #453)
171+ expect_true (x.value (0 ) == x[0 ]);
172+ expect_true (x.value (1 ) == x[1 ]);
173+ expect_true (x.value (2 ) == x[2 ]);
174+ }
158175
159176 test_that (" writable::integers(SEXP)" ) {
160177 SEXP x = PROTECT (Rf_allocVector (INTSXP, 5 ));
@@ -295,4 +312,56 @@ context("integers-C++") {
295312 int y = NA_INTEGER;
296313 expect_true (cpp11::is_na (y));
297314 }
315+
316+ test_that (" proxy issue demonstration" ) {
317+ // This test demonstrates the proxy issue and shows that all solutions work
318+ cpp11::writable::integers x;
319+ for (int i = 0 ; i < 3 ; i++) {
320+ x.push_back (i * 10 );
321+ }
322+
323+ // Test that value() method works correctly
324+ expect_true (x.value (0 ) == 0 );
325+ expect_true (x.value (1 ) == 10 );
326+ expect_true (x.value (2 ) == 20 );
327+
328+ // Test that explicit cast works
329+ expect_true ((int )x[0 ] == 0 );
330+ expect_true ((int )x[1 ] == 10 );
331+ expect_true ((int )x[2 ] == 20 );
332+
333+ // Test that auto assignment works (triggers implicit conversion)
334+ int val0 = x[0 ];
335+ int val1 = x[1 ];
336+ int val2 = x[2 ];
337+ expect_true (val0 == 0 );
338+ expect_true (val1 == 10 );
339+ expect_true (val2 == 20 );
340+
341+ // Test that value() and operator[] return equivalent results
342+ expect_true (x.value (0 ) == (int )x[0 ]);
343+ expect_true (x.value (1 ) == (int )x[1 ]);
344+ expect_true (x.value (2 ) == (int )x[2 ]);
345+ }
346+ }
347+
348+ // [[cpp11::register]]
349+ // Demo function to show the three ways to handle the proxy issue
350+ // To use this function:
351+ // 1. Run cpp11::cpp_register() to regenerate R bindings
352+ // 2. Rebuild and reinstall the package
353+ // 3. Call test_proxy_issue_demo() from R
354+ void test_proxy_issue_demo () {
355+ cpp11::writable::integers x;
356+ for (int i = 0 ; i < 5 ; i++) {
357+ x.push_back (i);
358+
359+ // These all work correctly:
360+ Rprintf (" Method 1 - cast: x[%d] = %d\n " , i, (int )x[i]);
361+ Rprintf (" Method 2 - value(): x[%d] = %d\n " , i, x.value (i));
362+
363+ // This also works (auto triggers implicit conversion):
364+ int val = x[i];
365+ Rprintf (" Method 3 - auto: x[%d] = %d\n " , i, val);
366+ }
298367}
0 commit comments