Advertisement

Unsafe const &in

Started by September 25, 2015 09:40 PM
4 comments, last by WitchLord 9 years, 1 month ago

Turning on unsafe references allows in-out references to be used for primitives and value types, meaning a function prototyped like:

void func(const ValueType &inout x)

will pass a reference to the function and not make a copy. This works just fine, but it seems like this should also work for a constant in reference:

void func(const ValueType &in x)

However, a copy will always be made for a value type passed to an in reference, even if the in reference is constant. This construct causes my users confusion, so I took a quick look at trying to modify the code in asCCompiler::PrepareArgument. The solution looks like it would be more complicated than simply changing what PrepareArgument does in response to an in reference. It looks like there are a lot of checks in the compiler that check the reference type and make assumptions about what an argument is allowed to be. Is this an accurate understanding?

It seems like the checks against the reference type in asCCompiler::MoveArgsToStack would also need to be modified, but I don't quite grasp what the new tests would be.

I would appreciate any help, thanks.

(P.S. It would also be nice to have a constant in-out reference be able to construct a temporary object if the argument passed in is found in the parameter type's constructor)

If you are using unsafe references then there is really no use for 'const ValueType &in'. It would be better to simply use 'const ValueType &'. If you want to customize the library I suggest you change it so that &in is modified to &inout when registering the function and unsafe references is turned. This way you don't have to make any changes in the compiler itself.

'const ValueType &in' should really be thought of passing the argument by value (rather than as a const reference). I already have plans to change this sometime in the future to make it clearer to the end-user.

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

Advertisement

I have followed your suggestion and changed const &in to become const &inout.

I do find the automatic construction of a temporary variable passed to a const &inout parameter useful, so I have attempted a modification to make that work. I have attached it as a patch. It seems to work in my tests, but I am not sure if I am properly handling all cases. If you don't incorporate this change officially, would you mind looking it over to see if I missed anything obvious to you?

Thanks

I'll take a look at what you've done.

You forgot to attach the patch though :)

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

I think my browser had some trouble with the file uploader.

Just reviewed the code. So far it seems you've thought of most of the things that needed to be modified.

To check if anything was broken in the process you'll need to run the regression tests (from the svn /tests/test_feature). You should preferably add new test cases for testing the implicit conversions in case of const &inout and unsafe references (e.g. in test_unsaferef.cpp).

One thing to remember (I'm not sure if you already thought of this or not) is that the const &inout should be referring to the actual object if the object is a ref type. This because it is possible that the function that receives the const reference may store a handle to it for later use. For value types it is not so important, as the function isn't allowed to keep the reference.

I may add this patch in sometime in the future with an engine property to allow users to turn it on or off as desired.

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

This topic is closed to new replies.

Advertisement