考虑这两个函数:
library(Rcpp)
cppFunction("NumericVector func1(NumericVector &x)
{
for (int i = 0; i < x.length(); i++)
x[i] = x[i] * 2;
return x;
}")
cppFunction("NumericVector func2(NumericVector x) // no &
{
for (int i = 0; i < x.length(); i++)
x[i] = x[i] * 2;
return x;
}")
唯一的区别是func1
将x
作为一个引用参数,而func2
将它作为一个值。如果这是常规的 C++,我会将其理解为 func1
被允许在调用代码中更改 x
的值,而这不会发生在 中func2
.
但是:
> x <- 1:10/5 # ensure x is numeric, not integer
> x
[1] 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0
> func1(x)
[1] 0.4 0.8 1.2 1.6 2.0 2.4 2.8 3.2 3.6 4.0
> x
[1] 0.4 0.8 1.2 1.6 2.0 2.4 2.8 3.2 3.6 4.0 # x in calling env has been modified
> x <- 1:10/5 # reset x
> x
[1] 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0
> func2(x)
[1] 0.4 0.8 1.2 1.6 2.0 2.4 2.8 3.2 3.6 4.0
> x
[1] 0.4 0.8 1.2 1.6 2.0 2.4 2.8 3.2 3.6 4.0 # x is also modified
就参数的副作用而言,看起来 func1
和 func2
的行为方式相同。
这是什么原因?一般来说,通过引用还是通过值将参数传递给 Rcpp 函数更好?
首先,您的两个函数都返回一个 NumericVector
,它没有分配给任何变量,因此没有被使用。下面的代码等同于您拥有的代码,因为无论如何您都将丢弃返回的 NumericVector
。
cppFunction("void func1(NumericVector& x)
{
for (int i = 0; i < x.length(); i++)
x[i] = x[i] * 2;
}")
cppFunction("void func2(NumericVector x) // no &
{
for (int i = 0; i < x.length(); i++)
x[i] = x[i] * 2;
}")
x <- 1:10/5
func1(x)
print(x)
x <- 1:10/5
func2(x)
print(x)
其次,NumericVector
在 C++ 函数中充当指针。指针为您提供存储值的地址,并且要能够更改该地址处的值,您只需要知道地址,但不需要修改地址本身的能力。因此,按值传递指针或按引用传递指针没有区别。
此线程包含有关 NumericVector
行为的有用知识:
Should I prefer Rcpp::NumericVector over std::vector?
下面的程序在 C++ 中演示了相同的行为。
#include <iostream>
void func1(double* a) // The pointer is passed by value.
{
for (int i=0; i<3; ++i)
a[i] *= 2;
}
void func2(double*& a) // The pointer is passed by reference.
{
for (int i=0; i<3; ++i)
a[i] *= 2;
}
void print(double* a)
{
std::cout << "Start print:" << std::endl;
for (int i=0; i<3; ++i)
std::cout << a[i] << std::endl;
}
int main()
{
double* x = new double[3];
// Set the values with 1, 2, and 3.
for (int i = 0; i<3; ++i)
x[i] = i+1;
print(x);
func1(x);
print(x);
// Reset the values with 1, 2, and 3.
for (int i = 0; i<3; ++i)
x[i] = i+1;
// This block shows the same behavior as the block above.
print(x);
func2(x);
print(x);
delete[] x;
}