ChaiScript array of objects without copy constructor


#1

I’ve got a bunch of objects that are, intentionally, not copyable. For the same of simplicity lets just say they’re of type X.

In ChaiScript I can do:

var x = X();
SomeCppFun(x);

just fine assuming SomeCppFun accepts a shared_ptr<X>. However, I have a function that accepts a vector<shared_ptr<X>> so I want to be able to do this:

var x1 = X();
var x2 = X();
// dies here
var xs = [x1, x2];
SomeCppFun(xs);

But I get errors on [x1, x2] that say, “Can not find appropriate ‘clone’ or copy constructor for vector elements” during evaluation". What I want is for ChaiScript to be putting references (shared_ptr’s) to x1 and x2 into that array. I couldn’t find anything that clarifies in the ChaiScript docs, but since JavaScript would have [x1, x2] as references to x1 and x2 (that is modifying xs[0] would modify x1) I assumed ChaiScript could create the array without copying the objects, but that appears not to be the case.

Is there any way to create arrays of references/pointers to non-copyable objects in ChaiScript?

Thanks!


#2

By the way, note that the constructor/function X() in the above is actually returning a shared_ptr<X>, not an X.


#3

I tried a few other things like xs = [&x1, &x2] but that doesn’t compile. Similarly, var x3 := x2; xs = [x3] still tries to make a copy of the item.

The only thing I can think of is making a wrapper type that has one and only 1 member which is a shared_ptr<X> and then the X() function returns that wrapper instead of a “real” X. That works but seems clunky. Hoping there’s an easier way…


#4

What does your function definition look like? Make sure it knows you’re passing a shared_ptr instead.

var := is what you’ll use for references.


#5

Thanks for the response @RobLoach. I don’t quite follow. There isn’t a function of mine involved here. I have two objects, x1, and x2 both of type X and X has deleted copy and assignment operators. I now want to create an array, in pure ChaiScript, of those two objects like xs = [x1, x2] but that doesn’t work - ChaiScript wants to copy them into the array. What I want to know is if it’s possible to create an array of references in ChaiScript.


#6

@oliverdain to use the code how you have it laid out, you’d have to do:

var container = []; // 
container.push_back_ref(x1);
container.push_back_ref(x2);

You might also try:

var container = [x(), x()];

But I’m not certain if that one would work without making a copy.


#7

Thanks @lefticus. That’s what I needed. Didn’t know about the push_back_ref method.