From 3fd3f384466aa918d17704770c4fbc76d0a0fbad Mon Sep 17 00:00:00 2001 From: Olivier Parcollet Date: Sun, 6 Apr 2014 12:24:32 +0200 Subject: [PATCH] clef: fix lazy method call for gcc - was not compiling on gcc (forward declaration issue). - now clearer - add a trivial print change in the mc class (to avoid trivial commit). --- doc/reference/c++/clef/overload.rst | 14 +++++++------- test/triqs/clef/lazy_method.cpp | 27 +++++++++++++++++++++++++++ test/triqs/clef/lazy_method.output | 5 ++++- triqs/clef/clef.hpp | 11 +++++------ triqs/mc_tools/mc_move_set.hpp | 2 +- 5 files changed, 44 insertions(+), 15 deletions(-) create mode 100644 test/triqs/clef/lazy_method.cpp diff --git a/doc/reference/c++/clef/overload.rst b/doc/reference/c++/clef/overload.rst index cb7e7d8d..94933c3e 100644 --- a/doc/reference/c++/clef/overload.rst +++ b/doc/reference/c++/clef/overload.rst @@ -95,10 +95,11 @@ Example : TRIQS_CLEF_IMPLEMENT_LAZY_CALL(); // a method - double my_method(double x) { return 2*x;} + double my_method(double x) const { return 2*x;} // CLEF overload - //TRIQS_CLEF_IMPLEMENT_LAZY_METHOD(Obj,my_method); + // WARNING : the method MUST be const + TRIQS_CLEF_IMPLEMENT_LAZY_METHOD(Obj,my_method); // Just to print itself nicely in the expressions friend std::ostream & operator<<(std::ostream & out, Obj const & x) { return out<<"Obj";} @@ -113,11 +114,10 @@ Example : std::cout << "Partial evaluation : "<< eval(f(y_) + 2*x_, y_=1) << std::endl ; std::cout << "Complete evalution : "<< eval(f(y_) + 2*x_, x_=3, y_=1) << std::endl< + +struct Obj { +double v; // put something in it +Obj(double v_): v(v_){} // constructor +Obj(Obj const &) = delete; // a non copyable object, to illustrate that we do NOT copy... + +// a method +double my_method(double x) const { return 2*x;} + +// CLEF overload +TRIQS_CLEF_IMPLEMENT_LAZY_METHOD(Obj,my_method); + +// Just to print itself nicely in the expressions +friend std::ostream & operator<<(std::ostream & out, Obj const & x) { return out<<"Obj";} +}; + +int main() { +Obj f(7); +triqs::clef::placeholder<1> x_; triqs::clef::placeholder<2> y_; + +std::cout << "Clef expression : "<< f.my_method(y_) + 2*x_ << std::endl ; +std::cout << "Complete evaluation : "<< eval(f.my_method(x_) + 2*x_, x_=1) << std::endl ; +std::cout << "Partial evaluation : "<< eval(f.my_method(y_) + 2*x_, y_=1) << std::endl ; +std::cout << "Complete evalution : "<< eval(f.my_method(y_) + 2*x_, x_=3, y_=1) << std::endl ; +} diff --git a/test/triqs/clef/lazy_method.output b/test/triqs/clef/lazy_method.output index 23a8303e..c43eefb6 100644 --- a/test/triqs/clef/lazy_method.output +++ b/test/triqs/clef/lazy_method.output @@ -1 +1,4 @@ -(eval( f.my_method(x_), x_ = 10)) ---> 20 +Clef expression : (apply_method:my_method(Obj, _2) + (2 * _1)) +Complete evaluation : 4 +Partial evaluation : (2 + (2 * _1)) +Complete evalution : 8 diff --git a/triqs/clef/clef.hpp b/triqs/clef/clef.hpp index 16fb0cde..f25a3667 100644 --- a/triqs/clef/clef.hpp +++ b/triqs/clef/clef.hpp @@ -566,15 +566,14 @@ namespace triqs { namespace clef { } #define TRIQS_CLEF_IMPLEMENT_LAZY_METHOD(TY, name) \ - struct __clef_lazy_method_impl_##name { \ - TY* _x; \ - template auto operator()(A&&... a) const DECL_AND_RETURN(_x -> name(std::forward(a)...)); \ - friend std::ostream& operator<<(std::ostream& out, __clef_lazy_method_impl_##name const& x) { \ - return out << BOOST_PP_STRINGIZE(TY) << "." << BOOST_PP_STRINGIZE(name); \ + struct __clef_lazy_method_impl_##TY##_##name { \ + template auto operator()(X&& x, A&&... a) const DECL_AND_RETURN(x.name(std::forward(a)...)); \ + friend std::ostream& operator<<(std::ostream& out, __clef_lazy_method_impl_##TY##_##name const& x) { \ + return out << "apply_method:"<< BOOST_PP_STRINGIZE(name); \ } \ }; \ template \ - auto name(A&&... a) DECL_AND_RETURN(make_expr_call(__clef_lazy_method_impl_##name{this}, std::forward(a)...)); + auto name(A&&... a) DECL_AND_RETURN(make_expr_call(__clef_lazy_method_impl_##TY##_##name{}, *this, std::forward(a)...)); #define TRIQS_CLEF_IMPLEMENT_LAZY_CALL(...) \ template \ diff --git a/triqs/mc_tools/mc_move_set.hpp b/triqs/mc_tools/mc_move_set.hpp index 88ba0126..1f5c9a48 100644 --- a/triqs/mc_tools/mc_move_set.hpp +++ b/triqs/mc_tools/mc_move_set.hpp @@ -200,7 +200,7 @@ namespace triqs { namespace mc_tools { double abs_rate_ratio; if (attempt_treat_infinite_ratio(rate_ratio, abs_rate_ratio)) { // in case the ratio is infinite if (!std::isfinite(std::abs(rate_ratio))) - TRIQS_RUNTIME_ERROR << "Monte Carlo Error : the rate is not finite in move " << name_of_currently_selected(); + TRIQS_RUNTIME_ERROR << "Monte Carlo Error : the rate ("<