Advertisement

asBEHAVE_CONSTRUCT with custom POD string type requires 'const'

Started by January 17, 2025 11:35 AM
4 comments, last by WitchLord 2 days, 4 hours ago

I use RegisterStringFactory to register my own string type, basically something similar to std::string_view.

So AS will see them as “const string_view” after creation.

The type is registerd with asOBJ_VALUE | asOBJ_POD | asGetTypeTraits<T>().

Now usually I pass my strings to functions by copy.

RegisterObjectMethod("obj", "void SetName(string_view)", asMETHOD(obj, SetName, asCALL_THISCALL);

->

obj.SetName("Jonny"); // works fine

So even though the string constant “Jonny” is treated as “const string_view”, this works.

EXCEPT in constrctors.

RegisterObjectBehaviour("obj", asBEHAVE_CONSTRUCT, "void f(string_view)", asFUNCTION(obj_ConstructView), asCALL_CDECL_OBJFIRST);

obj b("Jonny"); // DESON'T work

I get the error message, that there is no overload that takes “const string_view”. So if I change the behavior to this declaration, I get it to work:

RegisterObjectBehaviour("obj", asBEHAVE_CONSTRUCT, "void f(const string_view)", asFUNCTION(obj_ConstructView), asCALL_CDECL_OBJFIRST);

I really just add the “const” in the declaration.

Could this be an oversight? As far as I understand it, the constructor should also work without the const, since string_view should just be copied anyway.

Indeed. This looks like a bug in the compiler.

I'll investigate and have it fixed as soon as possible.

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've tried to reproduce this issue now, but wasn't successful.

Perhaps it was fixed already in another commit.

Can you verify if you still see the issue with the latest WIP version, and if so help me produce a sample code that reproduces the issue?

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

Yes, I can still reproduce this with the WIP version. Unfortunately, my time is currently quite limited, so not sure whether I can hack together a quick repo case in the near future.

I couldn't test this in depth, but here is what happens roughly:

void Func(ObjectType sv); // registered on C++ side, ObjectType is constructible from a string_view

Func("test"); // string factory returns const string_view -> doesn't work

string_view sv = "test";
Func(sv); // works, because it gets a non-const string_view and ObjectType accepts that for construction

Thanks. That was it. I was able to reproduce it now.

Working on identifying the fix.

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've fixed this in rev 3012

https://sourceforge.net/p/angelscript/code/3012/

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
Advertisement