Search code examples
c++functionlinkersizeof

The function call inside sizeof doesn't invoke it? C++


Consider this code, I am trying to print the sizeof return value of the function:

int f(int);

int main()
{
    std::cout << sizeof(f(2)) << std::endl;
}

This surprisingly (for me at least) prints 4.

But should this give me a link error as the function is not defined?
Are functions inside sizoef not invoked? Is there a broader rule for expressions inside sizeof?


Solution

  • Yes, according to cppreference:

    When applied to an expression, sizeof does not evaluate the expression [...]

    More on unevaluated expressions here