tbaederr · GitHub

See: https://godbolt.org/z/EY7f6M5bE

constexpr int foo(int (*bla)(void)) {
  return bla();
}
static_assert(foo(nullptr) == 1);

Output:

<source>:5:15: error: static assertion expression is not an integral constant expression
static_assert(foo(nullptr) == 1);
              ^~~~~~~~~~~~~~~~~
<source>:2:10: note: subexpression not valid in a constant expression
  return bla();
         ^
<source>:5:15: note: in call to 'foo(nullptr)'
static_assert(foo(nullptr) == 1);
              ^
1 error generated.

"subexpression not valid in a constant expression" is a pretty poor diagnostic for this case. When passing a non-constexpr function to foo(), clang manages to print a proper diagnostic:

<source>:9:15: error: static assertion expression is not an integral constant expression
static_assert(foo(f) == 1);
              ^~~~~~~~~~~
<source>:6:10: note: non-constexpr function 'f' cannot be used in a constant expression
  return bla();
         ^
<source>:9:15: note: in call to 'foo(&f)'
static_assert(foo(f) == 1);
              ^
<source>:2:5: note: declared here
int f() { return 1; }
    ^

Read the original on github.com ↗