sol2 icon indicating copy to clipboard operation
sol2 copied to clipboard

SOL_SAFE_NUMERICS and SOL_NO_CHECK_NUMBER_PRECISION

Open otristan opened this issue 5 years ago • 10 comments

Hi,

Basically I would like to be able to use the right overload when it exists but if there is no overload then that it default to classic cast of float to int.

Is that possible ?

Thanks !

otristan avatar Oct 22 '20 13:10 otristan

Write a function that takes double to capture all Lua numbers, and then cast + call your desired int-based function. There's no functionality to do this on a per-function level, but maybe I should add a cast_policy or figure out something similar...

ThePhD avatar Oct 22 '20 13:10 ThePhD

ok so basically I remove SOL_NO_CHECK_NUMBER_PRECISION and only keep SOL_SAFE_NUMERICS and handle manually the float to int overload. But yeah something built-in would be great :)

Basically I want something like in c++ Where it first look for the best candidate and then cast if necessary

otristan avatar Oct 22 '20 13:10 otristan

C++-style overloading is one of the most expensive kinds of overloading there is. "Best candidate" means I can't stop early when searching.

ThePhD avatar Oct 22 '20 13:10 ThePhD

Most of the time there are few overload though no ? and maybe this should be opt in so you know what are the tradeoff

otristan avatar Oct 22 '20 13:10 otristan

"Best match" overloading has been a thing I've been thinking about for a long time.

I should probably give it a shot here, conversions allowed and all.

ThePhD avatar Oct 28 '20 16:10 ThePhD

Slight out-loud think:

  • sol::overload_best_loose( f0, f1, f2, ... ); Allows conversions, needs a better name
  • sol::overload_best_strict( f0, f1, f2, ... ); Disallows conversions
  • sol::overload_best( f0, f1, f2, ... ); Disallows conversions (or maybe allows? We have both there, so picking a default behavior either way is okay and honestly is probably "whatever one is less effort" first)

ThePhD avatar Oct 28 '20 17:10 ThePhD

I wonder if this should be on a per function basis or just global like you did with SOL_SAFE_NUMERICS and SOL_NO_CHECK_NUMBER_PRECISION. In any case, both would suit me.

If you want to do it c++ style, the default could be to always allow all conversion (given a global define) and if you want you can force it to be strict on a per function basis (c++ explicit)

otristan avatar Oct 28 '20 17:10 otristan

Yeah, that is planned, but I still need a basic default for it. Like SOL_SAFE_OVERLOAD_BEST or something, where it defaults to being strict...

ThePhD avatar Oct 28 '20 17:10 ThePhD

Any progress on this ? :)

otristan avatar Oct 13 '21 15:10 otristan

Not something I can check in! Because, as it turns out, "best candidate" is obscenely hard and there's a reason why every single compiler engineer in the world hates C++-style overload resolution. :D

ThePhD avatar Oct 23 '21 19:10 ThePhD