Timo
Timo

Reputation: 9835

Is taking the address of an undefined function allowed?

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.

Upvotes: 6

Views: 157

Answers (1)

Artyer
Artyer

Reputation: 40891

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.

Upvotes: 10

Related Questions