Sign in to follow this  
ruzhenchao

Is it a feature or bug in lua or luabind?

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this