Search code examples
c++11clangstatic-assertnoexcept

C++11 static assertion fails noexcept check with Clang++?


I'm trying to compile the following code with clang++ -std=c++11 -c and it fails:

void g() noexcept {}

template <typename Func>
void f(Func && func) noexcept(noexcept(func()))
{ static_assert(noexcept(func()), "func()"); } // No error!

void h() { f(&g); } // No error!

static_assert(noexcept(f(&g)), "Error!");

The error message Clang 3.4.2 gives me is:

test.h:9:1: error: static_assert failed "Error!"
static_assert(noexcept(f(&g)), "Error!");
^             ~~~~~~~~~~~~~~~

What am I missing here?


Solution

  • noexcept is not a part of a function type.

    Thus, &g is just your run of the mill expression of type void(*)(), with no special noexcept powers. So is g, as it decays to the function pointer. When such a function pointer is eventually called, it has no noexcept specification, and thus the entire expression is not noexcept.