![]() |
Home | Libraries | People | FAQ | More |
In the Hello Calculator section, we looked into making calculator expressions directly usable as lambda expressions in calls to STL algorithms, as below:
double data[] = {1., 2., 3., 4.}; // Use the calculator EDSL to square each element ... HOW? std::transform( data, data + 4, data, _1 * _1 );
The difficulty, if you recall, was that by default Proto expressions
don't have interesting behaviors of their own. They're just trees. In
particular, the expression _1
* _1
won't have an operator()
that takes a double and returns a double like std::transform()
expects -- unless we give it one. To
make this work, we needed to define an expression wrapper type that defined
the operator()
member function, and we needed to associate the wrapper with the calculator
domain.
In Proto, the term domain refers to a type that associates expressions in that domain to an expression generator. The generator is just a function object that accepts an expression and does something to it, like wrapping it in an expression wrapper.
You can also use a domain to associate expressions with a grammar. When you specify a domain's grammar, Proto ensures that all the expressions it generates in that domain conform to the domain's grammar. It does that by disabling any operator overloads that would create invalid expressions.