.. highlight:: c Automatic assignment ======================= Principle ---------------- It is possible to use a subset of possible expressions as Left Hand Side (LHS) in an assignment statement, e.g. :: A(x_) = some_expression_of_x_ A[i_] = some_expression_of_i_ A(x_)(i_,j_) = some_expression_of_x_i_j A[x_](i_,j_) = some_expression_of_x_i_j * Right Hand Side (RHS) of the = statement can be any expression. * Left Hand Side (LHS) of the = sign. A must be a `lazy-assignable`, called by [] or (), one or several time. This writing means that a regular function :: x_ -> some_expression_of_x_ will be given to A so that it can fill itself by evaluating this function. A determines the range of value of x on which the function is called. Example --------- .. compileblock:: #include "triqs/clef.hpp" #include "triqs/clef/adapters/vector.hpp" #include int main() { int N = 10; double pi = std::acos(-1); std::vector V(N); // automatic assignment of vector and use of lazy math function triqs::clef::placeholder <0> k_; triqs::clef::lazy(V) [k_] << cos( (2* pi* k_)/ N ); // check result... for (size_t u=0; u void triqs_clef_auto_assign (Obj & x, Fnt f); The compiler will rewrite :: obj(x_,y_, ...) = expression into :: triqs_clef_auto_assign (obj, make_function( expression, x_, y_, ....)) The function is found by ADL. It is therefore useful to implement it e.g. as a friend function. Similarly, for [ ], adding a function :: template 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 : .. compileblock:: #include #include struct Obj{ double v; Obj(double v_): v(v_){} // lazy call : cf ... template< typename... Args> typename triqs::clef::result_of::make_expr_call,Args...>::type operator()( Args&&... args ) const { return make_expr_call (std::ref(* this),args...);} // The non const version (which then stores a non-const reference ....) template< typename... Args> typename triqs::clef::result_of::make_expr_call,Args...>::type operator()( Args&&... args ) { return make_expr_call (std::ref(* this),args...);} template friend void triqs_clef_auto_assign (Obj & x, Fnt f) { x.v++; std::cout<< " called triqs_clef_auto_assign "<< f(100)< x_; std::cout<< f.v << std::endl; f(x_ ) << 8*x_ ; //f(x_ + y_) = 8*x_; // leads to a compile error as expected std::cout<< f.v << std::endl; }