#Force runtime evaluation of a function

1 messages · Page 1 of 1 (latest)

vivid idol
#

How would I go about forcing the compiler to not optimize out a function, an have it only be evaluated at runtime? I am currently doing so by passing arguments that can only be evaluated at runtime like pointers, but that is not ideal.

royal wing
#

to make sure the function isnt' being run at comptime add this to the start of the function body

if(@inComptime()) @compileError("");
#

to make sure the compiler isn't optimizing the call out, maybe wrap the call like this: std.mem.doNotOptimizeAway(someFn());

vivid idol
#

Lets say my function returns a u32, how would I get the return value when wrapping th function in std.mem.doNotOptimizeAway?

royal wing
#
const x = someFn();
std.mem.doNotOptimizeAway(x):
vivid idol
#

thanks! Will try!