Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 342
Timo Avatar asked Apr 01 '20 18:04

Timo


People also ask

Why can't I read the property of undefined?

There are 3 main reasons the "Cannot read property of undefined" error occurs: Accessing a property on a variable storing an undefined value. Accessing a property on DOM element that doesn't exist. Inserting the JS script tag above the HTML, where the DOM elements are declared.

How to get the address of a function in C++?

We can get the address of a function by just writing the function’s name without parentheses. Please refer function pointer in C for details. In C/C++, name of a function can be used to find address of function. Writing code in comment? Please use ide.geeksforgeeks.org , generate link and share the link here.

Why destructor does not get the address in memory?

Conclusion:- so the conclusion is that due to exclusion of return type it will not get the address in memory. C++ constructor and destructor are not functions at all: they are macros. They get inlined into the scope where the object is created, and the scope where the object is destroyed.

What is an ADDRESSOF_expression?

An addressof_expression consists of an ampersand ( &) followed by a unary_expression. Given an expression E which is of a type T and is classified as a fixed variable ( Fixed and moveable variables ), the construct &E computes the address of the variable given by E. The type of the result is T* and is classified as a value.


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.

like image 82
Artyer Avatar answered Nov 09 '22 23:11

Artyer