Can I pass std::array?


#1

Hello,

Can I pass to Chaiscript std::array of my type?

Specfically, I would like to get this to work, but I cannot manage to make at function to work in ChaiScript. Cell class itself seems to work correctly.

using uft = std::function<void(std::array<grid::Cell, 8>)>;
std::array<grid::Cell, 8> cell_nbs;

chaiscript::ChaiScript chai(chaiscript::Std_Lib::library());

chai.add(chaiscript::user_type<grid::Cell>(), "Cell");
chai.add(chaiscript::fun(&grid::Cell::state), "get_state");
chai.add(chaiscript::fun(&grid::Cell::new_state), "get_new_state");

chai.add(chaiscript::user_type<std::array<grid::Cell, 8>>(), "NBS");
chai.add(chaiscript::fun(&std::array<grid::Cell, 8>::size), "size");
//something for function .at();

uft func =
        chai.eval<uft>("fun(n){"
                               "print(n.at(2).get_state());"
                               "} ");

func(cell_nbs);

#2

at and operator[] both have const and non-const versions. Is that the problem you are getting, a compile-time one regarding unresolved overloaded functions?

You can use a lambda to help resolve it:


//std::vector in chaiscript actually uses .at for "[]" like this example
chai.add(chaiscript::fun([](const std::array<grid::Cell, 8> &a, size_t loc){ return a.at(loc); }), "[]");
chai.add(chaiscript::fun([](std::array<grid::Cell, 8> &a, size_t loc){ return a.at(loc); }), "[]");

Does this help?


#3

I tried

chai.add(chaiscript::fun<grid::Cell& (std::array<grid::Cell, 8>::*)(size_t)>(&std::array<grid::Cell, 8>::at), "at");

following documentation, but I got compilation errors about operator().

error: 'operator()' is not a member of 'grid::Cell& (std::array<grid::Cell, 8ull>::*)(long long unsigned int)'

I’m using GCC 6.1

Your code works, thanks much!