Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 365
jotik Avatar asked Nov 01 '22 18:11

jotik


1 Answers

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.

like image 93
n. m. Avatar answered Jan 04 '23 13:01

n. m.