Hacker News new | ask | show | jobs
by mgaunard 1169 days ago
By forcing the call to happen at compile-time, he would get an error.

The input being potentially known at compile-time is not sufficient to make a constexpr function be called at compile-time.

1 comments

Yes, but that error would simply complain that a variable that doesn't have a compile time known value is been used in a compile time context.

Conversely, the origin of the thread implies that if a function has been successfully called with some argument from a static_assert, it will not have UB at runtime even if called with some other arguments. This subthread was showing that this is not the case, and that you can't test all uses of a function using static_assert to guarantee that it will not exhibit UB.

In case it's not clear, this piece of code will also fail to compile, even though it exhibits no UB:

  constexpr int foo(int y) {
    return y;
  } 
  int y;
  std::cin>>y;
  static_assert(foo(y));
https://godbolt.org/z/xvhP8nq7z