3
0
mirror of https://github.com/triqs/dft_tools synced 2024-11-01 19:53:45 +01:00
dft_tools/doc/reference/c++/clef/assign.rst
Olivier Parcollet 3fe400d34c doc : split c++ code from rst
- examples split from the rst file using a python script (split_code).
- Final result for the doc is unchanged.
- examples are compiled and tested with the other tests.
- examples' code have been clang-formatted, with triqs style.
- doc compiles much faster, and with the same options as the rest of the
  test.
- examples are added as tests, so they are run by make test, as simple C
  tests.
- done for the tutorials and the reference.
- autocompile removed (changed into triqs_example directive).
- add triqs_example :
   - make a literal include of the source code.
   - runs the compiled example
   - add, as before, the result to the source code in the doc.
- added the script split_code, used to make the changes automatically,
  maybe for later reuse. (in _tools)
2014-05-31 23:00:16 +02:00

70 lines
1.9 KiB
ReStructuredText

.. highlight:: c
.. _clef_auto_assign:
Automatic assignment of containers
===================================
Another use of expression is the automatic assignment of containers.
**Synopsis** :
If C is a container, ::
C(x_) << some_expression_of_x_
C[i_] << some_expression_of_i_
C(x_)(i_,j_) << some_expression_of_x_i_j
C[x_](i_,j_) << some_expression_of_x_i_j
depending of course of the operator that the container support.
The Right Hand Side (RHS) of the << statement can be any expression,
while Left Hand Side (LHS) of the << sign is a container supporting the operation (see below).
This statement simply will be rewritten by the CLEF library as ::
triqs_clef_auto_assign (C, x_ -> some_expression_of_x_); // pseudo code
triqs_clef_auto_assign (C, [](auto x_) {return some_expression_of_x_;}); // or in C++ lambda syntax
The function `triqs_clef_auto_assign` has to be overloaded for the container and the correct
functional form, and it is expected to fill C by evaluating this function.
Such a function is provided for TRIQS objects (arrays, matrix, Green function),
and also for some STL container like std::vector.
Example :
.. triqs_example:: ./assign_0.cpp
**Details**
The synopsis of the `triqs_clef_auto_assign` functions is ::
template<typename Fnt> void triqs_clef_auto_assign (Obj & x, Fnt f);
The compiler will then rewrite ::
obj(x_,y_, ...) = expression
into ::
triqs_clef_auto_assign (obj, make_function( expression, x_, y_, ....))
The function must be found by ADL. It is therefore useful to implement it e.g. as a friend function.
Similarly, for [ ], adding a function ::
template<typename Fnt> void triqs_clef_auto_assign_subscript (Obj & x, Fnt f);
The compiler will rewrite ::
obj[i_] = expression
into ::
triqs_clef_auto_assign_subscript (obj, make_function( expression, i_))
A complete example :
.. triqs_example:: ./assign_1.cpp