gpt4 book ai didi

c++ - 如何实际使用表达式模板

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

the wikipedia article ,它提供了一些模板类。我想在实际代码中使用它。我怎样才能做到这一点?我发现我几乎没有办法实例化一个 Vec 对象。

最佳答案

Wikibooks article on Expression Templates提供更多见解,尤其是最后一部分:

The above example does not show how recursive types are generated at compile-time. Also, expr does not look like a mathematical expression at all, but it is indeed one. The code that follows show how types are recursively composed using repetitive instantiation of the following overloaded + operator.

template< class A, class B >
DExpression<DBinaryExpression<DExpression<A>, DExpression<B>, Add> >
operator + (DExpression<A> a, DExpression<B> b)
{
typedef DBinaryExpression <DExpression<A>, DExpression<B>, Add> ExprT;
return DExpression<ExprT>(ExprT(a,b));
}

The above overloaded operator+ does two things - it adds syntactic sugar and enables recursive type composition, bounded by the compiler's limits. It can therefore be used to replace the call to evaluate as follows:

evaluate (a.begin(), a.end(), x + l + x); 
/// It is (2*x + 50.00), which does look like a mathematical expression.

关于c++ - 如何实际使用表达式模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8497515/

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