Boost logo

Boost Users :

Subject: Re: [Boost-users] [proto] learning proto slowly, a small (math)complex library
From: alfC (alfredo.correa_at_[hidden])
Date: 2011-01-10 16:42:53


> See the boostcon slides of Eric last year. All you need is to give your
> complex expression an operator to cast itself to std::complex in which
> you evaluate the expression real and imag part and retrun the newly
> constructed std::complex out of it.

great, nice resource.

I translated the map_list_of example into the complex example. There
is still work to do, the code doesn't work yet. I guess I have to
still tell proto that "complex_cartesian_expr" (didn't know what other
name to put) is of type proto::plus< proto::terminal<double> ,
proto::multiplies<proto::terminal<double>, proto::terminal<i_tag> >
>.
I don't know how to do that. complete code at the end of the email.

> Incidentally, i am using this very same example in a courses on DSEL
> design in C++ i am starting to write atm.

I knew it could be a natural example since most people know simple
complex arithmetic and std::complex<double>.

> So i can shed some light if
> you want.

That would be great. Any help is greatly appreciated and I promise my
feedback.

----
#include <boost/proto/core.hpp>
#include <complex>
namespace pretty{
namespace proto = boost::proto;
struct i_tag{};
proto::terminal< i_tag >::type const i = {{}};
template< typename Expr >
struct complex_cartesian_expr;
struct complex_cartesian_domain
: proto::domain< proto::generator< complex_cartesian_expr > >
{};
template<typename Expr>
struct complex_cartesian_expr :
	proto::extends< Expr, complex_cartesian_expr< Expr >,
complex_cartesian_domain >{
	complex_cartesian_expr( Expr const & expr = Expr() ) :
	proto::extends< Expr, complex_cartesian_expr< Expr >,
complex_cartesian_domain >( expr ){}
	template<class K, class V, class C, class A>
	operator std::complex<K>() const{
		std::complex<K> m(proto::value( proto::child_c<1>(*this) ),
proto::value( proto::child_c<1>(proto::child_c<2>(*this)) ) );
		return m;
	}
};
}
int main(){
	using namespace pretty;
	std::complex<double> z(4. + 5.*i);
	return 0;
}

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net