我是 Rcpp 的新手,所以仍然盲目地寻找我的方式。问题的长短是我有一个生成指针的对象,我希望该指针返回给 R。
我发现将指针转换为 a 可以size_t
保持必要的精度,但是,我似乎无法使用wrap
.
在下面的代码中,只返回unsigned long int
将编译,其他的会抛出错误,为了篇幅我不在这里包含。对于我的对象,转换为 anunsigned long int
会导致编译器由于精度丢失而失败(即在第一个块中全部被注释掉)。
使用 a应该足以满足我的需要,以避免为此类对象size_t
创建模板的替代方法。wrap
我检查了更改日志,似乎应该支持 size_t 。该概述还建议wrap
支持size_t
.
#include <Rcpp.h>
#include <iostream>
using namespace Rcpp;
using namespace std;
extern "C" SEXP attempt()
{
// this block if uncommented gives compile error that converting a pointer to unsigned long int loses precision
// also, wrapping the pointer &f causes a compilation error
//int f = 314;
//unsigned long int theVar_longint = (unsigned long int) &f;
//cout << "pointer: " << &f << endl;
//return(wrap(&f));
// This block makes an arbitrary value into a size_t, unsigned long int and unsigned long long int
size_t theVar_sizet = (size_t) 383762523;
unsigned long int theVar_longint = (unsigned long int) 383762523;
unsigned long long int theVar_longlongint = (unsigned long long int) 383762523;
// prints the results
cout << "size_t: " << theVar_sizet << endl;
cout << "longint: " << theVar_longint << endl;
cout << "longlongint: " << theVar_longlongint << endl;
// only the first line returns properly, the others cause errors in compilation
return(wrap(theVar_longint));
//return(wrap(theVar_longlongint));
//return(wrap(theVar_sizet));
}