Hacker News new | ask | show | jobs
by mgaunard 1167 days ago
That seems unrelated to the sub-thread in question.
1 comments

The example program is calling foo(y), where the value of y is read from standard input. You suggested to evaluate that expression at compile time, which is not possible. How is this unrelated to the thread?
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.

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