sol2 icon indicating copy to clipboard operation
sol2 copied to clipboard

Using references for container parameters

Open vdweller84 opened this issue 3 years ago • 2 comments

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?

vdweller84 avatar Feb 07 '23 13:02 vdweller84

Could you try passing by const reference instead:

void ShaderSetUniformF(ShaderID ID, const std::string& name, const std::vector<float>& values);

deadlocklogic avatar Apr 10 '23 19:04 deadlocklogic

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.

roman-orekhov avatar Jun 27 '23 16:06 roman-orekhov