Skip to content

Commit aed58c1

Browse files
jeroenclaude
andcommitted
Fix ambiguous Vector::operator[] on platforms without LONG_VECTOR_SUPPORT
On wasm32 (and other platforms where LONG_VECTOR_SUPPORT is not defined) R_xlen_t is int while ptrdiff_t is long. Subscripting an Rcpp::Vector with a long index then becomes ambiguous between the member operator[] (which needs a long -> int conversion on the index) and the built-in pointer subscript operator[](SEXPREC*, ptrdiff_t) synthesised via the implicit Vector -> SEXP conversion (which needs a user-defined conversion on the object but matches the index exactly). The two implicit conversion sequences are mutually unrankable so overload resolution fails. Add a long-index overload that is only compiled when LONG_VECTOR_SUPPORT is not defined, so LP64 builds (where R_xlen_t == long already) are unaffected. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 8d2f8f8 commit aed58c1

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

ChangeLog

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
2026-06-23 Jeroen Ooms <jeroen@berkeley.edu>
2+
3+
* inst/include/Rcpp/vector/Vector.h (Vector::operator[]): Add a
4+
long-index overload on platforms without LONG_VECTOR_SUPPORT (such
5+
as wasm32) to avoid ambiguity with the built-in pointer subscript
6+
synthesised via the implicit Vector -> SEXP conversion
7+
18
2026-06-18 Dirk Eddelbuettel <edd@debian.org>
29

310
* vignettes/rmd/Rcpp.bib: Updated references

inst/include/Rcpp/vector/Vector.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,14 @@ class Vector :
337337

338338
inline Proxy operator[]( R_xlen_t i ){ return cache.ref(i) ; }
339339
inline const_Proxy operator[]( R_xlen_t i ) const { return cache.ref(i) ; }
340+
#ifndef LONG_VECTOR_SUPPORT
341+
// On platforms without long vector support (notably wasm32) R_xlen_t is
342+
// int while ptrdiff_t is long, which makes c[i] with a long index
343+
// ambiguous between this operator[] and the built-in pointer subscript
344+
// synthesised via the implicit Vector -> SEXP conversion.
345+
inline Proxy operator[]( long i ){ return cache.ref(i) ; }
346+
inline const_Proxy operator[]( long i ) const { return cache.ref(i) ; }
347+
#endif
340348

341349
inline Proxy operator()( const size_t& i) {
342350
return cache.ref( offset(i) ) ;

0 commit comments

Comments
 (0)