Sign in to follow this  
Followers 0
zexee

Why need to copy when using const T &in

5 posts in this topic

HI,

 

First of all, I am looking for such (c++ style) script for a long time and very happy that angelscript exists.

 

I am working on design even simpler interface to register c++ objects / functions so that I can easily register many good c++ libraries and make it a general purpose language.

 

I met some problem when dealing with arguments passing to functions. The const T&in type will always cause a copy constructing for the object passed in and the object will be destroyed after the function ends, and const T&out will create a new object to pass into the function and copy it back to the original object after the function ends. These two behaviors happen no matter for value object type or reference object type. These behaviors are different from c++ and will cause some problem.

 

Since for reference object type, neither const T &inout or T &inout do the copy and I think const T&in should be the same as const T &inout, so that is not a implementation problem.

 

I am wondering what's the original thought of &in and &out? Since the caller will always hold a reference or the value so the object will not be destroyed during the call, and const will control whether the function can modify it or not, so why do we need &in and &out?

0

Share this post


Link to post
Share on other sites

The real reason for &in and &out is to support sandboxing, which means that the script language must guarantee that references and handles are always valid.

 

In C++ it is quite easy to write code that invalidates a reference passed to a function. This is OK for C++ because the programmer is responsible for making sure everything is working ok, but with scripting it is not always possible to assume the script writer will be responsible (or even have a clear understanding over how things work).

 

// C++
vector<string> g_array;
void foo(string &value)
{
   g_array.clear();
   value = "crash since the reference is no longer valid";
}
void main()
{
   g_array.push_back("a");
   foo(g_array[0]);
}

 

Within the script itself &in doesn't make much sense, since the end result is the same as passing the argument by value. But in order to maintain compatibility with C++ where many input arguments are passed by & for performance reason it is needed to differentiate between by value and input reference.

 

&out is needed as it is the only way a function can give additional return values.

 

 

Now, if you don't care about sand-boxing you can change the behaviour of AngelScript by setting the engine property asEP_ALLOW_UNSAFE_REFERENCES to true. When this is set you will be able to use &inout on all types, including primitives and registered value types, which makes it equivalent to C++.

1

Share this post


Link to post
Share on other sites

Thank you very much for the detailed answer! It prevents the passed in arguments being destroyed inside the current function. If it is a reference object, the argument always holds a reference, so &inout is allowed. 

 

I tried asEP_ALLOW_UNSAFE_REFERENCES, and it works very well, except for it still disallow register a reference cast behavior to a value type. Since it is already unsafe, this cast should be no more unsafe than passing value type by ref right?

0

Share this post


Link to post
Share on other sites

For example, I want to have a

template<int N>
class Vector;

I want some specific method for specific Vector<N>, for example CrossProduct are only for Vector<3>. But also, all Vector can have some common method like operator+(). So here is what I implemented

template<int N>
class VectorBase{ /* common methods */}
template<int N>
class Vector : public VectorBase;
template<>
class Vector<3> : public VectorBase<3> { /* specific methods */ }

The operator+ can only return a VectorBase, so many functions only need to accept VectorBase& as arguments. So I want Vector& be implicit cast to VectorBase&.

0

Share this post


Link to post
Share on other sites

Thanks for the example. 

 

I'll take this into consideration when deciding whether or not to add the implicit ref casts for value types.

0

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  
Followers 0