quick_start.qbk
来自「Boost provides free peer-reviewed portab」· QBK 代码 · 共 82 行
QBK
82 行
[/ / Copyright (c) 2008 Eric Niebler / / Distributed under the Boost Software License, Version 1.0. (See accompanying / file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) /][section Hello World]Below is a very simple program that uses Proto to build an expression templateand then execute it. #include <iostream> #include <boost/xpressive/proto/proto.hpp> #include <boost/xpressive/proto/context.hpp> #include <boost/typeof/std/ostream.hpp> using namespace boost; proto::terminal< std::ostream & >::type cout_ = { std::cout }; template< typename Expr > void evaluate( Expr const & expr ) { proto::default_context ctx; proto::eval(expr, ctx); } int main() { evaluate( cout_ << "hello" << ',' << " world" ); return 0; }This program outputs the following:[prehello, world]This program builds an object representing the output operation and passesit to an `evaluate()` function, which then executes it.The basic idea of expression templates is to overload all the operators sothat, rather than evaluating the expression immediately, they build a tree-likerepresentation of the expression so that it can be evaluated later. For eachoperator in an expression, at least one operand must be Proto-ified in orderfor Proto's operator overloads to be found. In the expression ... cout_ << "hello" << ',' << " world"... the Proto-ified sub-expression is `cout_`, which is the Proto-ification of`std::cout`. The presence of `cout_` "infects" the expression, and brings Proto's tree-building operator overloads into consideration. Any literals inthe expression are then Proto-ified by wrapping them in a Proto terminal beforethey are combined into larger Proto expressions.Once Proto's operator overloads have built the expression tree, the expressioncan be lazily evaluated later by walking the tree. That is what `proto::eval()`does. It is a general tree-walking expression evaluator, whose behavior iscustomizable via a /context/ parameter. The use of _default_context_ assignsthe standard meanings to the operators in the expression. (By using a differentcontext, you could give the operators in your expressions different semantics.By default, Proto makes no assumptions about what operators actually /mean/.)[note [*Proto Design Philosophy]Before we continue, let's use the above example to illustrate an importantdesign principle of Proto's. The expression template created in the ['helloworld] example is totally general and abstract. It is not tied in any way toany particular domain or application, nor does it have any particular meaningor behavior on its own, until it is evaluated in a /context/. Expressiontemplates are really just heterogeneous trees, which might mean something inone domain, and something else entirely in a different one.As we'll see later, there is a way to create Proto expression trees that are['not] purely abstract, and that have meaning and behaviors independent of anycontext. There is also a way to control which operators are overloaded for yourparticular domain. But that is not the default behavior. We'll see later whythe default is often a good thing.][endsect]
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?