Insert Into Map With No Default Constructor


#1

In C++ I have

std::map< std::string, chaiscript::Boxed_Value> variables;

Inside a class. I register that variables member to chaiscript and then later try to run this script (assume self is the object with the exposed variables member):

class VoidElementalCombat {
	def VoidElementalCombat(self){
		this.self := self;
	}

	def update(dt){
		print("UPDATE");
	}

	var self;
	var target;
	var countdownToTarget;
}

self.variables.emplace(“combat”, VoidElementalCombat(self));

The issue I’m having is emplace isn’t picked up. I could do self.variables[“combat”] = VoidElementalCombat(); if there were a default constructor, but I’m wondering if I’m left having to create a custom binding to do the emplace or if chaiscript should offer some mechanism here?


#2

ChaiScript should be smart enough to avoid copying an object into the variables if it knows it is a return value. So, you should be able to get what you want by just doing:

self.variables["combat"] = VoidElementalCombat(self);

#3

It wasn’t working in a 5.7.? build, but it does in 5.8 :smile:


#4

I’m not shocked. I spent a lot of time fixing my version of ‘move semantics’ in the 5.7/5.8 releases.