[proto] vec3 sample error

I found a strange error when I try the vec3 sample from proto, my code is like this: struct Vec3SubscriptCtx : proto::callable_context< Vec3SubscriptCtx const > { typedef int result_type; Vec3SubscriptCtx(int i) : i_(i) {} // Index array terminals with our subscript. Everything // else will be handled by the default evaluation context. int operator ()(proto::tag::terminal, int const (&arr)[3]) const { return arr[this->i_]; } int i_; }; struct Vec3 : proto::extends<proto::terminal<int[3]>::type, Vec3> { explicit Vec3(int i=0, int j=0, int k=0) { (*this)[0] = i; (*this)[1] = j; (*this)[2] = k; } int &operator [](int i) { return proto::value(*this)[i]; } int const &operator [](int i) const { return proto::value(*this)[i]; } // Here we define a operator = for Vec3 terminals that // takes a Vec3 expression. template< typename Expr > Vec3 &operator =(Expr const & expr) { typedef Vec3SubscriptCtx const CVec3SubscriptCtx; (*this)[0] = proto::eval(proto::as_expr(expr), CVec3SubscriptCtx(0)); (*this)[1] = proto::eval(proto::as_expr(expr), CVec3SubscriptCtx(1)); (*this)[2] = proto::eval(proto::as_expr(expr), CVec3SubscriptCtx(2)); return *this; } template< typename Expr > Vec3 (Expr const & expr) { typedef Vec3SubscriptCtx const CVec3SubscriptCtx; (*this)[0] = proto::eval(proto::as_expr(expr), CVec3SubscriptCtx(0)); (*this)[1] = proto::eval(proto::as_expr(expr), CVec3SubscriptCtx(1)); (*this)[2] = proto::eval(proto::as_expr(expr), CVec3SubscriptCtx(2)); } void print() const { std::cout << '{' << (*this)[0] << ", " << (*this)[1] << ", " << (*this)[2] << '}' << std::endl; } }; int main(int argc, const char* argv[]) { Vec3 a(1, 2, 3); Vec3 b(3, 2, 1); Vec3 c; c = b; c.print(); return 0; } I expected that when c is printed it will display 3, 2, 1 ( the value of b ) but instead it print 0,0,0 like b is never assigned to c. I try this using vs 2010 with boost 1.44 in windows 7 64.

On 11/2/2010 10:14 AM, Kamil Zubair wrote:
I found a strange error when I try the vec3 sample from proto, my code is like this: <snip>
int main(int argc, const char* argv[]) { Vec3 a(1, 2, 3); Vec3 b(3, 2, 1); Vec3 c; c = b; c.print();
return 0; }
I expected that when c is printed it will display 3, 2, 1 ( the value of b ) but instead it print 0,0,0 like b is never assigned to c. I try this using vs 2010 with boost 1.44 in windows 7 64.
You found a bug in the Vec3 example. The Vec3 class needs a the following copy assign: Vec3 &operator=(Vec3 const &that) { (*this)[0] = that[0]; (*this)[1] = that[1]; (*this)[2] = that[2]; return *this; } Why? A template operator= is never considered for copy assignment, and so it finds the one in proto::extends. That one simply creates an assign tree node which never gets used. Thanks for the report. I'll fix the example. -- Eric Niebler BoostPro Computing http://www.boostpro.com
participants (2)
-
Eric Niebler
-
Kamil Zubair