Skip to content

Instantly share code, notes, and snippets.

@Thell
Last active Aug 29, 2015
Embed
What would you like to do?
// [[Rcpp::plugins("cpp11")]]
#include <ostream>
#include <iterator>
#include <RcppCommon.h>
class BadIter;
RCPP_EXPOSED_CLASS(BadIter)
#include <Rcpp.h>
using namespace Rcpp;
class BadIter {
public:
std::vector< int > vec;
std::vector< int >::iterator vec_iter;
void print_addr()
{
std::cout << " vec.begin addr: " << std::addressof(*vec.begin())
<< std::endl
<< " vec_iter addr: " << std::addressof(*vec_iter)
<< std::endl;
}
BadIter(int len) : vec(len), vec_iter( vec.begin() )
{
std::cout << "Initializing: " << std::endl;
print_addr();
}
std::vector< int > get()
{
std::cout << "Accessing: " << std::endl;
print_addr();
std::cout << "Reset: " << std::endl;
vec_iter = vec.begin();
print_addr();
return vec;
}
}; // end class: BadIter
BadIter make_BadIter( int n ) { return BadIter(n); }
RCPP_MODULE(BadIter){
class_< BadIter >("BadIter")
.constructor< int >()
.method( "access", &BadIter::get )
;
function( "make_BadIter", &make_BadIter );
}
// [[Rcpp::export]]
void rcpp_BadIter(int n)
{
auto bi = BadIter(n);
bi.get();
}
/*** R
# Via Rcpp
rcpp_BadIter(1)
# Via R
bi <- make_BadIter(1)
bi$access( )
# Setting the vec_iter to vec_begin _after_ the constructor has completed.
bi$access( )
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment