Is taking the address of an undefined function allowed?

Viewed 101

The following code defines the entire program. Is this program standard conformant (with the latest version)?

void foo();

int main()
{
    auto x = &foo;
    return 0;
}

Here is a convenience shortcut.

This code has no practical use, I'm just curious.

1 Answers

Every function that is odr-used requires a definition (no diagnostic required). Taking the address of a function is an odr-use, so this program exhibits undefined behaviour. With optimisations, the address-of is optimised out so it doesn't have a linker error, but it's not required to work.

Related