Advertisement

Capturing return value by `const &`

Started by August 03, 2021 03:46 AM
2 comments, last by swiftcoder 3 years, 4 months ago

Hello,

My understanding is that something like the following is not possible:

string myString = "hi";
const string& getString()
{
	return myString;
}

...

void main()
{
	const string& theString = getString();
}

I was wondering why we aren't able to capture the return value by const reference?

Correct, it is currently not possible.

The problem is with how to guarantee the life time of the object. In your example it is obvious for the programmer that the global variable would stay in valid for the full scope of the local variable theString. But for the compiler it is not trivial to know this.

In C++ the responsibility of guaranteeing the validity of the reference is on the programmer, but in AngelScript the compiler must guarantee this.

I already have some thoughts in the to-do list (look for ‘Variables declared as &’) on how support for this might be implemented in AngelScript, but I haven't done any attempts to implement it so far.

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

This topic is closed to new replies.

Advertisement