Last active
August 29, 2015 13:56
-
-
Save Thell/9231866 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// [[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