在Eigen中移动语义 [英] Move semantics in Eigen

查看:115
本文介绍了在Eigen中移动语义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个关于Eigen的问题:

I have couple of question about Eigen:


  1. 有没有人知道是否有任何计划支持移动语义任何时候很快?在Eigen3网页的TODO列表上找不到任何内容。现在我使用 swap 技巧来摆脱临时性,例如

  1. Does anyone know if there is any plan to support move semantics in Eigen anytime soon? Couldn't find anything on the TODO list of the Eigen3 web page. Right now I am using the swap trick to get rid of temporaries, like

MatrixXd foo()
{
    MatrixXd huge_matrix(N,N); // size N x N where N is quite large
    // do something here with huge_matrix
    return huge_matrix; 
}

MatrixXd A(N, N); 
A.swap(foo());

我非常想写上面的 swap line在C ++ 11风格像

I'd very much like to write the above swap line in a C++11 style like

A = foo();

,而不必担心 foo / code>。

and not to have to worry about the temporary returned by foo().


推荐答案

Copy elision 会做得很好。即使是C ++ 03编译器也会删除临时的。

特别是,对于这个代码,NRVO(命名的返回值优化)将会

Copy elision will do the job just fine. Even a C++03 compiler will elide the temporary.
In particular, NRVO (named return value optimization) will, for this code,

MatrixXd foo()
{
    MatrixXd huge_matrix(N,N);
    return huge_matrix; 
}

MatrixXd A = foo();

构建 huge_matrix c> A 。 C ++ 03标准在[class.copy] / 15中指定:

construct huge_matrix right inside A. The C++03 standard specifies in [class.copy]/15:


这种复制操作的精确度允许在以下
情况(可能结合以消除多个副本):


  • > return 语句在一个具有类返回类型的函数中,当表达式是具有与函数返回类型相同的cv非限定类型的非易失性自动对象的名称时,
    当未被绑定到引用(12.2)的临时类对象被复制时,通过将自动对象
    直接构造到函数的返回值

  • 到具有相同
    cv-unsqualified类型的类对象,通过构造
    可以省略复制操作将临时对象直接插入到省略副本的目标中

  • in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object with the same cv-unqualified type as the function return type, the copy operation can be omitted by constructing the automatic object directly into the function’s return value
  • when a temporary class object that has not been bound to a reference (12.2) would be copied to a class object with the same cv-unqualified type, the copy operation can be omitted by constructing the temporary object directly into the target of the omitted copy

这篇关于在Eigen中移动语义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆