ThePhD / sol2

Sol3 (sol2 v3.0) - a C++ <-> Lua API wrapper with advanced features and top notch performance - is here, and it's great! Documentation:
http://sol2.rtfd.io/
MIT License
4.12k stars 500 forks source link

Using references for container parameters #1452

Open vdweller84 opened 1 year ago

vdweller84 commented 1 year ago

Hello,

Suppose I have this function in C++ : void ShaderSetUniformF(ShaderID ID, const std::string& name, std::vector<float> values);

Now in lua, if I write: ShaderSetUniformF(shader_multi,"col",{0.1, 0.1, 0.4, 1.0})

Things work as expected.

My question is if this is not the best way to do this, as I am using the values parameter by, erm, value, and if there is any better way. I'm asking this because, for instance, void ShaderSetUniformF(ShaderID ID, const std::string& name, std::vector<float>& values); doesn't work (I get an error).

So, is there a good way to pass containers around by reference and still work?

deadlocklogic commented 1 year ago

Could you try passing by const reference instead:

void ShaderSetUniformF(ShaderID ID, const std::string& name, const std::vector<float>& values);
roman-orekhov commented 1 year ago

Lua tables are far from being C++ std::vectors. Also Lua uses doubles and your std::vector contains floats, so vector references can't possibly point to the original table.