RcppArmadillo 的示例是否需要预先实例化参数?

Does RcppArmadillo's sample require argument to be instantiated beforehand?

我在我的 Rcpp 代码中使用 RcppArmadillo::sample,它在下面有这种奇怪的行为。 fun_good 按预期工作,从 x 向量中采样 1 个元素。但是,fun_bad 不起作用,即使唯一的区别是我没有事先创建源向量 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);

虽然我无法提供发生这种情况的明确原因,但如果您明确声明 seq_len 输出是 IntegerVector,则该函数会按预期编译和运行。

// [[Rcpp::export]]
IntegerVector fun_bad() {
  IntegerVector newOffer = RcppArmadillo::sample((IntegerVector)seq_len(5), 1, true);
  return newOffer;
}

我认为这是因为 sample 中的 seq_len 调用是一个临时对象,因此是 rvalue。将结果显式转换为 IntegerVector 似乎使其成为 lvalue 因此它起作用的原因。同样,我不知道为什么会这样。

我敢肯定,如果您等待足够长的时间,Dirk 或 C++ 知识比我多的人最终会过来给出更明确的答案。

TL;DR

进行显式转换(如 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 是一个表达式模板 class -- 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 classes 不同,SeqLen 更像是一个 "true" 表达式模板,因为它只提供了一个 operator[] 因为被懒惰地评估。它不存储常量引用数据成员/提供向量转换运算符(例如 cumprod and many others do); it is literally used to construct a 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 class 中定义的以下辅助方法:

// 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 表达式自动生成一个实际的向量对象之前,它是不可使用的(至少在这个特定表达式中需要的方式是:ret[ii] = x[jj];).


†就像完整性检查一样,我们可以使用一些 C++11 元编程结构来检查 SeqLen::operator[]Vector::operator[] 的 return 值之间的差异:

// [[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

*/