我在我的 Rcpp 代码中使用 RcppArmadillo::sample
,它在下面有这种奇怪的行为。 fun_good
按预期工作,从 x
vector 中采样 1 个元素。然而,fun_bad
不起作用,即使唯一的区别是我没有事先创建源 vector x
。
#include <RcppArmadilloExtensions/sample.h>
// [[Rcpp::depends(RcppArmadillo)]]
using namespace Rcpp;
// [[Rcpp::export]]
IntegerVector fun_good() {
IntegerVector x = seq_len(5);
IntegerVector newOffer = RcppArmadillo::sample(x, 1, true);
return newOffer;
}
// [[Rcpp::export]]
IntegerVector fun_bad() {
IntegerVector newOffer = RcppArmadillo::sample(seq_len(5), 1, true);
return newOffer;
}
错误消息是lvalue required as left operand of assignment
,并指向以下来源。为什么 ret[ii]
不能在 fun_bad
中赋值?
// copy the results into the return vector
for (ii=0; ii<size; ii++) {
jj = index[ii];
ret[ii] = x[jj];
}
return(ret);
最佳答案
长话短说
进行显式转换(如 cdeterman 所做的那样)或显式构造函数调用:
// [[Rcpp::export]]
Rcpp::IntegerVector fun_bad() {
Rcpp::IntegerVector newOffer =
RcppArmadillo::sample(Rcpp::IntegerVector(seq_len(5)), 1, true);
return newOffer;
}
不要在细节上引用我的话,但我很确定你遇到过表达式模板不能很好地处理模板类型推导规则的边缘情况。首先,我的编译器发出的错误消息的相关部分:
... In instantiation of ‘T Rcpp::RcppArmadillo::sample(const T&, int, bool, Rcpp::NumericVector) [with T = Rcpp::sugar::SeqLen; ...
因此,在 templated sample
function , T
被推断为具有类型 Rcpp::sugar::SeqLen
.
SeqLen
是一个表达式模板类 -- defined here -- 在大多数情况下,它会(隐式地)转换为 Rcpp::IntegerVector
(由于它继承自 Rcpp::VectorBase<INTSXP, ...>
)。例如,
// [[Rcpp::export]]
Rcpp::IntegerVector test(int n = 5) {
return Rcpp::seq_len(5); // Ok
}
但是,由于隐式转换是重载解析过程的一部分,而不是模板类型推导过程,T
完全推导为 Rcpp::sugar::SeqLen
-- 意思是这个表达式
ret[ii] = x[jj];
正在调用 Rcpp::sugar::SeqLen::operator[]
(并且不是 Rcpp::Vector::operator[]
通常是这种情况),它会产生一个右值(见下文†)。
您可能已经注意到,与某些 ET sugar
不同类,SeqLen
更像是一个“真实的”表达式模板,因为它只提供了一个 operator[]
因为被懒惰地评估。它不存储常量引用数据成员/不提供 vector 转换运算符(例如 cumprod
和许多其他运算符);它实际上是用来构造一个 vector -- this constructor如果我没记错的话,
template <bool NA, typename VEC>
Vector( const VectorBase<RTYPE,NA,VEC>& other ) {
RCPP_DEBUG_2( "Vector<%d>( const VectorBase<RTYPE,NA,VEC>& ) [VEC = %s]", RTYPE, DEMANGLE(VEC) )
import_sugar_expression( other, typename traits::same_type<Vector,VEC>::type() ) ;
}
它使用在 Vector
中定义的以下辅助方法类:
// we are importing a real sugar expression, i.e. not a vector
template <bool NA, typename VEC>
inline void import_sugar_expression( const Rcpp::VectorBase<RTYPE,NA,VEC>& other, traits::false_type ) {
RCPP_DEBUG_4( "Vector<%d>::import_sugar_expression( VectorBase<%d,%d,%s>, false_type )", RTYPE, NA, RTYPE, DEMANGLE(VEC) ) ;
R_xlen_t n = other.size() ;
Storage::set__( Rf_allocVector( RTYPE, n ) ) ;
import_expression<VEC>( other.get_ref() , n ) ;
}
template <typename T>
inline void import_expression( const T& other, int n ) {
iterator start = begin() ;
RCPP_LOOP_UNROLL(start,other)
}
无论如何,直到 Rcpp 从 sugar::SeqLen
自动生成一个实际的 vector 对象表达式,它不可用(至少在这个特定表达式中需要的方式:ret[ii] = x[jj];
)。
†就像完整性检查一样,我们可以使用一些 C++11 元编程结构来检查 SeqLen::operator[]
的返回值之间的差异。和 Vector::operator[]
:
// [[Rcpp::plugins(cpp11)]]
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadilloExtensions/sample.h>
typedef decltype(Rcpp::sugar::SeqLen(1)[0]) rvalue_t;
typedef decltype(Rcpp::IntegerVector::create(1)[0]) lvalue_ref_t;
// [[Rcpp::export]]
void test() {
// rvalue_t is an rvalue
Rcpp::Rcout
<< std::is_rvalue_reference<rvalue_t&&>::value
<< "\n";
// lvalue_ref_t is an lvalue
Rcpp::Rcout
<< std::is_lvalue_reference<lvalue_ref_t>::value
<< "\n";
// rvalue_t is _not_ assignable
Rcpp::Rcout
<< std::is_assignable<rvalue_t, R_xlen_t>::value
<< "\n";
// lvalue_ref_t is assignable
Rcpp::Rcout
<< std::is_assignable<lvalue_ref_t, R_xlen_t>::value
<< "\n";
}
/*** R
test()
# 1 ## true
# 1 ## true
# 0 ## false
# 1 ## true
*/
关于c++ - RcppArmadillo 是否需要预先实例化所需的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37396162/