Is it a feature or bug in lua or luabind?

Started by
1 comment, last by ruzhenchao 14 years, 8 months ago
Hi! I am using lua and luabind now! It works well but have little problem. That is redundant data generation! for example:

  class Vector2
  {
   public:
     float x,y;
   public:
     Vector2():x(0.0),y(0.0){}
     Vector2(float xx,float yy):x(xx),y(yy)
     {
        std::cout<<"Vector2(float xx,float yy) called"<<std::endl;
     }
     Vector2(const Vector2& vect):x(vect.x),y(vect.y)
     {
        std::cout<<"Vector2(const Vector2& vect) called"<<std::endl;
     }
     ~Vector2()
     {
        std::cout<<"~Vector2 called"<<std::endl;
     }
     Vector2 operator+(const Vector2& vect)
     {
         return Vector2(x+vect.x , y+vect.y);
     }
     void set(const Vector2& vect)
     {
         x = vect.x;
         y = vect.y;
     }
  }


the lua code is like this:

 vec1 = Vector2(10,10);
 vec2 = Vector2(100,100);
 vec3 = Vector2();
 vec3:set(vec1+vec2);


then the result is:

Vector2(float xx,float yy) called    //vec1 
Vector2(float xx,float yy) called    //vec2
Vector2(float xx,float yy) called    //the Vector2 in operator+(const Vector2&)
Vector2(const Vector2& vect) called  //the redudant data,call set function
~Vector2 called                      //the Vector2 in op + destory;
~Vector2 called                      //may be the redudant data destory
~Vector2 called                      //may be the vec2 destory;
~Vector2 called                      //may be the vec1 destory;

if I use this code in c++,the redudant data will not generate So is it a feature of lua or luabind?? Thanks!
Advertisement
Technically neither; when you generate the code in C++ the compiler can optimise the output to avoid these copies.

When it comes to late binding of functions, as you are doing with Lua and LuaBind the compiler can't optimise these things away.
So I think maybe your are right!
Thanks !

This topic is closed to new replies.

Advertisement