gpt4 book ai didi

c++ - 使用表达式模板的中间结果

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:41:04 26 4
gpt4 key购买 nike

C++ 模板元编程:来自 Boost 及其他的概念、工具和技术

... One drawback of expression templates is that they tend to encourage writing large, complicated expressions, because evaluation is only delayed until the assignment operator is invoked. If a programmer wants to reuse some intermediate result without evaluating it early, she may be forced to declare a complicated type like:

Expression<
Expression<Array,plus,Array>,
plus,
Expression<Array,minus,Array>
> intermediate = a + b + (c - d);

(or worse). Notice how this type not only exactly and redundantly reflects the structure of the computationand so would need to be maintained as the formula changes but also overwhelms it? This is a long-standing problem for C++ DSELs. The usual workaround is to capture the expression using type erasure, but in that case one pays for dynamic dispatching. There has been much discussion recently, spearheaded by Bjarne Stroustrup himself, about reusing the vestigial auto keyword to get type deduction in variable declarations, so that the above could be rewritten as:

auto intermediate = a + b + (c - d);

This feature would be a huge advantage to C++ DSEL authors and users alike...

是否可以使用当前的 c++ 标准解决此问题。 (非 C++0X)

例如我想写一个像这样的表达式:

表达式 X,Y

矩阵 A,B,C,D

X=A+B+C

Y=X+C

D:=X+Y

Where operator := 在最近的时间计算表达式。

最佳答案

目前,您始终可以使用 BOOST_AUTO()代替 C++0x 的 auto 关键字更容易获得中间结果。

Matrix x, y;
BOOST_AUTO(result, (x + y) * (x + y)); // or whatever.

关于c++ - 使用表达式模板的中间结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1666176/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com