Does Chai has "for a in map" style loops?



Does Chai has “for a in map” style loops?



Not directly, it’s one of the current feature requests for “more modern loops”

You can use a built in for_each functional-style algorithm, like:

map.for_each(fun(elem) {
  // do something with elem.first
  // do something with elem.second


Here’s the related feature request:


Thank you for fast reply.

Is it possible to write loop something like iterator style?
for (std::map<std::string, int>::iterator it = map.begin();it!=map.end();It++)

And we experience issues with maps with pointer data type. We can’t access the data. Like std::map<std::string, UserClass *>

We register it as Map type, and can access it, but not the “second” elements. I will provide sample as it will be possible


I try voodoo and black magic. It crashes and I have no idea what is wrong

typedef std::map<std::string, int*> map_type_pointer;

int local_i;
map_type_pointer tmp = { { "ss", &local_i } };
chai.add(chaiscript::var(&tmp), "tmp");

var j = tmp["ss"]

We have tons of maps string->object pointer :frowning:


What kind of crash do you get? Memory error? Uncaught exception? If it’s an exception, what’s the type/description? If it’s a memory error, what’s the stack trace look like?



I will try to revert all my changes now, to look what it was. But what I have found. If this map is map of shared_ptr< int> - it works, if it is map of int* - it raise exception.

Will report details soon


Does it helps?


Not 100% helpful, knowing the text of that exception would be best. However, I have a hunch from the info you gave that if you try:

var j := tmp["ss"]; // reference assignment vs copy assignment.

It will work, as that will prevent the system from attempting a clone() operation