防止表达式模板绑定到右值引用 [英] Prevent expression templates binding to rvalue references
问题描述
我明白做了以下事情:
auto&& x = Matrix1()+ Matrix2()+ Matrix3();
std :: cout<< x(2,3) std :: endl;如果矩阵操作使用表达式模板(例如 http://www.boost.org/doc/libs/1_49_0/libs/numeric/ublas/doc/index.htm> boost :: ublas
)。
有没有任何方法来设计表达式模板,以防止编译器编译这样的代码,从而导致在运行时使用过期的临时表达式?
(我尝试不成功解决此问题,尝试是此处)
解决方案
有任何方式设计表达式模板,以防止编译器编译这样的代码,在运行时使用过期临时表?
否。这在C ++ 11的最终标准化之前已经被确认,但是我不知道是否被提请委员会注意。不是一个修复会很容易。我想最简单的事情是类型上的标志,如果 auto
尝试推导它,但是即使这将是复杂的,因为 decltype
也可以推导它,以及模板参数扣除。所有这三个都以相同的方式定义,但你可能不想让后者失败。
只是记录你的库,希望没有人试图捕捉他们的方式。
I understand that doing something like the following:
auto&& x = Matrix1() + Matrix2() + Matrix3();
std::cout << x(2,3) << std::endl;
Will cause a silent runtime error if the matrix operations use expression templates (such as boost::ublas
).
Is there any way of designing expression templates to prevent the compiler from compiling such code that may result in the use of expired temporaries at runtime?
(I've attempted unsuccessfully to work around this issue, the attempt is here)
解决方案
Is there any way of designing expression templates to prevent the compiler from compiling such code that may result in the use of expired temporaries at runtime?
No. This was actually recognized before C++11's final standardization, but I don't know if it was ever brought to the committee's notice. Not that a fix would have been easy. I suppose the simplest thing would be a flag on types that would simply error if auto
tries to deduce it, but even that would be complex because decltype
can also deduce it, as well as template argument deduction. And all three of these are defined in the same way, but you probably don't want the latter to fail.
Just document your library appropriately and hope that nobody tries to capture them that way.
这篇关于防止表达式模板绑定到右值引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!