Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


- - - - -

Why need to copy when using const T &in


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 zexee   Members   -  Reputation: 118

Like
0Likes
Like

Posted 25 September 2013 - 11:36 AM

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?



Sponsor:

#2 Andreas Jonsson   Moderators   -  Reputation: 3452

Like
1Likes
Like

Posted 25 September 2013 - 01:59 PM

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++.


AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

#3 zexee   Members   -  Reputation: 118

Like
0Likes
Like

Posted 25 September 2013 - 11:41 PM

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?



#4 Andreas Jonsson   Moderators   -  Reputation: 3452

Like
0Likes
Like

Posted 26 September 2013 - 08:02 AM

Perhaps, but that is not implemented.

 

Why do you need a reference cast for a value type?


AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

#5 zexee   Members   -  Reputation: 118

Like
0Likes
Like

Posted 28 September 2013 - 08:18 AM

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&.



#6 Andreas Jonsson   Moderators   -  Reputation: 3452

Like
0Likes
Like

Posted 29 September 2013 - 10:23 AM

Thanks for the example. 

 

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


AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS