Comment

Comment by arcticbull

I'm saying a conditional branch that offers a different path of execution for a constant evaluation than for a runtime evaluation gives you a function that behaves differently in different contexts.

In Rust, for instance, constant functions have to evaluate the same in both compile time and run time - you can't have a different code path depending on which. If you want that, make two functions.

Trivially:

  constexpr bool is_even(int x) {
    if (std::is_constant_evaluated()) {
      return true;
    } else {
      return ((x % 2) == 0);
    }
  }
How would you unit test this in a way that catches that only the constant evaluation is broken?
parent arcticbull 4 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton