Error, cannot assign to temporary value


#1

Hello guys.
I’ve been trying to get results from a query in the database returning the values in a map<string, boxed_value> and I get the error: Error, cannot assign to temporary value_

chaisql.cpp:

[code]std::map<std::string, chaiscript::Boxed_Value> ChaiSql::Query(const std::string& Query)
{
std::map<std::string, chaiscript::Boxed_Value> Result;
auto ToLower = [](std::string Name) -> std::string {
std::transform(Name.begin(), Name.end(), Name.begin(), tolower);
return Name;
};

for (auto i : this->_db->Query(Query))
    {
        Result.insert({ToLower(i.first), chaiscript::Boxed_Value(i.second)});
    }

    return Result;

}[/code]

chaiengine.cpp:

[code]utility::add_class(*m, “fdSql”,
{
constructor<ChaiSql()>()
},
{
{fun(&ChaiSql::Query), “query”},
});

this->_script->add(m);
this->_script->add_global(var(Singleton::Instance()), “sql”);[/code]

main.chai:

[code]// works fine
var result = sql.query(“select name from player where id = '”+ player.account() + “’”)

if (!result.empty()) { // works fine
    var name = result["name"] // error
     // or
    if (result["name"] == "name") { // error
    }
    
    return true
}

return false

[/code]
Am I doing something wrong? The documentation is a bit complicated for my understanding, out I’ve tried to do this work in many other ways and still nothing.


#2

I’ve tried to reproduce your error and cannot seem to.

The only case I see where I get an error in ChaiScript is if result["name"] is empty and the result from it is an undefined value, then the assignment and == would cause runtime errors.

Is it possible this is the case you are running into? Can you verify that result["name"] is returning a non-empty value and what type it is?


#3

I’ve added this lambda to test. I’ve added a breakpoint in return and the variable “Value” is coming with the correct name, but I can’t use.
The type of result is string, but to send the chai, I converted to Boxed_Value.

this->_script->add(fun([](const std::string& value) -> bool { return !_stricmp(Value.c_str(), "test") ? true : false; }), "test_value");

And whenever the lambda tried to return the result, I get this error … So I remade several times the function because at first I thought the function was the problem, but then later I’ve tried to use only the map of the result in the chaiscript and I received the same error. So I figured that the error was on the map and not in function.


#4

Would it be possible to post a complete example that demonstrates the problem? The example from your first post relies on some DB backend code that I don’t have access to.

If you can reduce the problem to the smallest example possible, that would help a lot.


#5

I have changed a few things and I did some more tests and I get the same error.
My code changes:

Function query:

std::map<std::string, std::string> ChaiSql::Query(const std::string& Query) { return this->_db->Query(Query); // function Query return std::map<std::string, std::string> with key lowercase digits }

Added to my module:

bootstrap::standard_library::map_type<std::map<std::string, std::string>>("MapOfString", m); this->_script->add(m); // This is on the end of the my load function in my class ChaiEngine.

Debugging prints:

main.chai:

Running to_bool() function from the script and checking the value:

Returning a result of to_bool() function:

I’ve tried with to_int() too and also got the same error.


#6

It’s really quite impossible for me to debug the issue if I cannot reproduce it.

I need two specific things from you:

  1. Please verify that you are using the latest official version of ChaiScript (5.8.3 https://github.com/ChaiScript/ChaiScript/releases/tag/v5.8.3)
  2. Please provide a complete stand alone example that reproduces the issue.