mirror of
https://github.com/triqs/dft_tools
synced 2024-12-25 13:53:40 +01:00
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).
This commit is contained in:
parent
efb00ea5d3
commit
3fd3f38446
@ -95,10 +95,11 @@ Example :
|
|||||||
TRIQS_CLEF_IMPLEMENT_LAZY_CALL();
|
TRIQS_CLEF_IMPLEMENT_LAZY_CALL();
|
||||||
|
|
||||||
// a method
|
// a method
|
||||||
double my_method(double x) { return 2*x;}
|
double my_method(double x) const { return 2*x;}
|
||||||
|
|
||||||
// CLEF overload
|
// 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
|
// Just to print itself nicely in the expressions
|
||||||
friend std::ostream & operator<<(std::ostream & out, Obj const & x) { return out<<"Obj";}
|
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 << "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<<std::endl ;
|
std::cout << "Complete evalution : "<< eval(f(y_) + 2*x_, x_=3, y_=1) << std::endl<<std::endl ;
|
||||||
|
|
||||||
// not implemented on all compilers
|
std::cout << "Clef expression : "<< f.my_method(y_) + 2*x_ << std::endl ;
|
||||||
//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 << "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 << "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 ;
|
||||||
//std::cout << "Complete evalution : "<< eval(f.my_method(y_) + 2*x_, x_=3, y_=1) << std::endl ;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
**NB** When the method or the non CLEF operator() is already a template,
|
**NB** When the method or the non CLEF operator() is already a template,
|
||||||
|
27
test/triqs/clef/lazy_method.cpp
Normal file
27
test/triqs/clef/lazy_method.cpp
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
#include "./common.hpp"
|
||||||
|
#include <triqs/clef.hpp>
|
||||||
|
|
||||||
|
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 ;
|
||||||
|
}
|
@ -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
|
||||||
|
@ -566,15 +566,14 @@ namespace triqs { namespace clef {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#define TRIQS_CLEF_IMPLEMENT_LAZY_METHOD(TY, name) \
|
#define TRIQS_CLEF_IMPLEMENT_LAZY_METHOD(TY, name) \
|
||||||
struct __clef_lazy_method_impl_##name { \
|
struct __clef_lazy_method_impl_##TY##_##name { \
|
||||||
TY* _x; \
|
template <typename X, typename... A> auto operator()(X&& x, A&&... a) const DECL_AND_RETURN(x.name(std::forward<A>(a)...)); \
|
||||||
template <typename... A> auto operator()(A&&... a) const DECL_AND_RETURN(_x -> name(std::forward<A>(a)...)); \
|
friend std::ostream& operator<<(std::ostream& out, __clef_lazy_method_impl_##TY##_##name const& x) { \
|
||||||
friend std::ostream& operator<<(std::ostream& out, __clef_lazy_method_impl_##name const& x) { \
|
return out << "apply_method:"<< BOOST_PP_STRINGIZE(name); \
|
||||||
return out << BOOST_PP_STRINGIZE(TY) << "." << BOOST_PP_STRINGIZE(name); \
|
|
||||||
} \
|
} \
|
||||||
}; \
|
}; \
|
||||||
template <typename... A> \
|
template <typename... A> \
|
||||||
auto name(A&&... a) DECL_AND_RETURN(make_expr_call(__clef_lazy_method_impl_##name{this}, std::forward<A>(a)...));
|
auto name(A&&... a) DECL_AND_RETURN(make_expr_call(__clef_lazy_method_impl_##TY##_##name{}, *this, std::forward<A>(a)...));
|
||||||
|
|
||||||
#define TRIQS_CLEF_IMPLEMENT_LAZY_CALL(...) \
|
#define TRIQS_CLEF_IMPLEMENT_LAZY_CALL(...) \
|
||||||
template <typename... Args> \
|
template <typename... Args> \
|
||||||
|
@ -200,7 +200,7 @@ namespace triqs { namespace mc_tools {
|
|||||||
double abs_rate_ratio;
|
double abs_rate_ratio;
|
||||||
if (attempt_treat_infinite_ratio(rate_ratio, abs_rate_ratio)) { // in case the ratio is infinite
|
if (attempt_treat_infinite_ratio(rate_ratio, abs_rate_ratio)) { // in case the ratio is infinite
|
||||||
if (!std::isfinite(std::abs(rate_ratio)))
|
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 ("<<rate_ratio<<") is not finite in move " << name_of_currently_selected();
|
||||||
abs_rate_ratio = std::abs(rate_ratio);
|
abs_rate_ratio = std::abs(rate_ratio);
|
||||||
#ifdef TRIQS_TOOLS_MC_DEBUG
|
#ifdef TRIQS_TOOLS_MC_DEBUG
|
||||||
std::cerr << " Metropolis ratio " << rate_ratio << ". Abs(Metropolis ratio) " << abs_rate_ratio << std::endl;
|
std::cerr << " Metropolis ratio " << rate_ratio << ". Abs(Metropolis ratio) " << abs_rate_ratio << std::endl;
|
||||||
|
Loading…
Reference in New Issue
Block a user