Why does printing a function name returns a value?

后端 未结 3 1879
野趣味
野趣味 2021-01-12 15:41

I accidentally printed a function name without the parenthesis and it printed a value. I am just curious about how this happens? Output is same irrespective of the function

3条回答
  •  -上瘾入骨i
    2021-01-12 16:01

    std::cout << foo;
    

    Outputs 1 because foo is a function pointer, it will be converted to bool with std::cout.

    To print its address, you need explicitly cast it:

    std::cout << reinterpret_cast(foo) << std::endl;
    

    With printf("\n%u", foo);, %u expects unsigned int, what you saw is the value of the function pointer converted to unsgigned int.

提交回复
热议问题