🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Capturing return value by `const &`

Started by
2 comments, last by swiftcoder 2 years, 10 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?

Advertisement

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

This topic is closed to new replies.

Advertisement