expression template 表达式模板

来源:互联网 发布:java和.net的区别 编辑:程序博客网 时间:2024/06/05 17:31

Expression Template---表达式模板


  • What is expression template?

Expression templates is a C++ template metaprogramming technique in which templates are used to represent part of an expression. Typically, the template itself represents a particular type of operation, while the parameters represent the operands to which the operation applies. The expression template can then be evaluated at a later time, or passed to a function. [1]


  • How can it help you?

1. To create a domain-specific embedded language (DSEL) in C++
2. To support lazy evaluation of C++ expressions (e.g., mathematical expressions), which can be executed much later in the program from the point of their definition.
3. To pass an expression -- not the result of the expression -- as a parameter to a function. [2]


  • Principle:

Expression templates use the Recursive Type Composition idiom. Recursive type composition uses instances of class templates that contain other instances of the same template as member variables. Multiple repetitive instantiation of the same template gives rise to an abstract syntax tree (AST) of types. Recursive type composition has been used to create linear Type lists as well as binary expression trees. [2]


  • Use case:

/**@file "expression_template.hpp"@brief Expression template@author C.Zhou, Eric@date 2012.3*/#if !defined(IDEA_TEST_EXPRESSION_TEMPLATE_HPP)#define IDEA_TEST_EXPRESSION_TEMPLATE_HPPnamespace ExprTemplate{//operand, represent a variablestruct var{double operator() (double d) const {return d;}};//operand, represent a constantstruct constant{explicit constant(double d) : m_d(d){}double operator() (double) const{return m_d;}double m_d;};//expression, this makes the binary operation can be lazily evaluated.template <typename Var0, typename Var1, typename OP>struct binary_expression{binary_expression(const Var0& var0, const Var1& var1) : m_var0(var0), m_var1(var1){}double operator() (double d) const{return OP::apply(m_var0(d), m_var1(d));}Var0 m_var0;Var1 m_var1;};//operation, addstruct add{static double apply(double d0, double d1) {return d0 + d1;}};//expression in form of templatetemplate <typename Expr>struct expression{explicit expression(const Expr& expr) : m_expr(expr){}double operator() (double d) const{return m_expr(d);}Expr m_expr;};}//ExprTemplate#endif//IDEA_TEST_EXPRESSION_TEMPLATE_HPP

/**@file "expression_template.cpp"@brief Expression template@author C.Zhou, Eric@date 2012.3*/#include "expression_template.hpp"#include <vector>#include <iostream>namespace ExprTemplateTest{template <typename Iter, typename Func>void evaluate(Iter begin, Iter end, Func func){for(Iter iter = begin; iter != end; ++iter)std::cout << func(*iter) <<std::endl;}void test(){using namespace ExprTemplate;typedef expression<var>var_expr_t;typedef expression<constant>const_expr_t;typedef binary_expression<var_expr_t, const_expr_t, add>var_const_adder_t;typedef expression<var_const_adder_t>my_adder_t;var my_var;var_expr_t var_x(my_var);const_expr_t const_x(constant(4.8));var_const_adder_t v_c_adder(var_x, const_x);my_adder_t expr(v_c_adder);std::vector<double> data;data.push_back(1.2);data.push_back(10.8);evaluate(data.begin(), data.end(), expr);}}//ExprTemplateTest

/**@file "main.cpp"@brief Expression template@author C.Zhou, Eric@date 2012.3*/int _tmain(int argc, _TCHAR* argv[]){ExprTemplateTest::test();return 0;}



Ref:
[1]: http://en.wikipedia.org/wiki/Expression_templates
[2]: http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Expression-template
Dig:
http://blog.csdn.net/ralph623/article/details/505190
http://www.angelikalanger.com/Articles/Cuj/ExpressionTemplates/ExpressionTemplates.htm

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 淘宝买东西退货卖家拒绝怎么办? 淘宝被限制下单怎么办 淘宝扫码登录后怎么办 花呗选项被隐藏怎么办 新换手机支付宝怎么办 ih5点击按钮跳转页面怎么办 sap点安装程序没反应怎么办 淘宝店账号忘了怎么办 点击电脑桌面图标没反应怎么办 为什么淘宝打不开已停止运行怎么办 移动宽带打不开淘宝网怎么办 淘宝发布宝贝没有品牌怎么办 烫了卷发显老怎么办 唯品会商品不支持退换货怎么办 京东没有自提怎么办 不支持七天无理由的商品怎么办 淘宝评价忘记点亮星星怎么办 淘宝宝贝权重降低了该怎么办 换卡支付宝账号怎么办 支付宝免密支付无法关闭怎么办 手机换了支付宝怎么办 支付宝版本过低怎么办 支付宝无故扣钱怎么办 支付宝里面的钱不见了怎么办 支付宝还不起钱怎么办 支付宝借不到钱怎么办 买家淘宝号被监控了怎么办 卖微信号被骗了怎么办 淘宝原密码忘了怎么办 淘宝号忘记密码了怎么办啊 淘宝忘了原密码怎么办 电脑账户密码忘了怎么办 淘宝的密码忘了怎么办 支付宝支付异常无法登录怎么办 笔记本电脑打不开光盘怎么办 电脑dvd弹不出来怎么办 安卓手机浏览器有个锁退不了怎么办 华硕电脑com端口没有怎么办 淘宝子账号认证过于频繁怎么办 现在手机淘宝卖家怎么办 注册新的淘宝账户怎么办