Rcpp and move semantic
Rcpp and move semantic
我在C++
中实现了一个算法,该算法返回一个巨大的元素数组作为输出。现在,我想在Rcpp
中实现一个包装器,以便能够使用R
调用此函数。
我在Makevars文件中指定了以下设置:
PKG_CXXFLAGS=-std=c++11
这样我就可以使用C++11版本了。
// [[Rcpp::export]]
NumericMatrix compute(int width, int height)
{
vector<data_t> weights(width * height);
compute_weights(weights);
NumericMatrix mat(height, width);
copy(begin(weights), end(weights), mat.begin());
return mat;
}
如果NumericMatrix在函数返回时被移动,则上述包装函数仍然有效,否则将创建一个新对象。
Rcpp
是否利用了移动语义?如果没有,有没有办法避免构建副本?
如果NumericMatrix在函数返回时被移动,则上述包装函数仍然有效,否则将创建一个新对象。
如果没有,有没有办法避免构建副本?
我认为副本构造函数只创建了一个浅副本,所以不应该有任何副本。请参阅Rcpp:如何确保NumericMatrix的深度复制?和
- https://github.com/RcppCore/Rcpp/blob/b3b0bea7403d9836397148fa310b86eb24923aba/inst/include/Rcpp/vector/Matrix.h#L74
- 哪个调用https://github.com/RcppCore/Rcpp/blob/b3b0bea7403d9836397148fa310b86eb24923aba/inst/include/Rcpp/vector/Vector.h#L62-L67
这个例子也证实了这一点
#include <Rcpp.h>
// [[Rcpp::export]]
Rcpp::NumericVector allocate_the_vec(R_xlen_t n_ele){
Rcpp::NumericVector out(n_ele);
return out;
}
/*** R
# got 16 GB ram on my laptop. 3 x 7 is an issue but 2 x 7 is not
how_large <- as.integer(7 * 10^9 / 8)
the_large_vec_1 <- allocate_the_vec(how_large)
object.size(the_large_vec_1)
the_large_vec_2 <- allocate_the_vec(how_large)
*/
相关文章:
- 瓦尔格林德:数学函数"Conditional jump or move depends on uninitialised value(s)"
- Usages of std::move
- 在C++中对T*类型执行std::move的意外行为
- 关于std::move的使用,是否有编译警告
- 我应该实现右值推送功能吗?我应该使用std::move吗
- 通过实例理解std::move及其目的
- 使用仅使用一次的变量调用的复制构造函数.这可能是通过调用move构造函数进行编译器优化的情况吗
- 是否可以在 C++03 中定义'move-and-swap idiom'等效项
- 返回一个带有 std::move 的对象并链接函数
- '[](std::list& list)<int>{return std::move(list)}(list)' 是否保证将 'list' 留空?
- 为什么字符串的 move() 会改变内存中底层数据的位置?
- 当 std::move 与 C 样式数组或不移动对象时会发生什么
- 为什么当我为 for(auto& it : myUnorderedMap) {... = std::move(it.second)} 时,我会得到一个 const 引用?
- 添加自定义析构函数时,Move 构造函数在派生类中消失
- 将向量从 N1 缩小到 N2 项,而不触发默认构造函数并仅使用 move 语义
- CPP 中的瓦尔格林德和记忆泄漏:"Conditional jump or move depends on uninitialised values"
- std::move a const std::vector in a lambda capture
- "std::forward"和"std::move"真的不生成代码吗?
- std::vector move 而不是交换到空 vector 并释放存储
- Rcpp and move semantic