"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Why Does Printing a Function Name in C++ Result in \"1\"?

Why Does Printing a Function Name in C++ Result in \"1\"?

Published on 2024-11-08
Browse:406

Why Does Printing a Function Name in C   Result in \

couting a Function Without Calling: Unraveling the Mystery

Imagine this: you're coding away and instead of invoking a function with parentheses, you simply print its name. Surprisingly, the result is always 1. This unconventional approach leaves you perplexed, both about the 1s and the absence of the expected function pointer.

Let's delve into the intricacies of your code:

#include 
using namespace std;

void pr()
{
    cout 

Contrary to your intention, you're not actually calling the function pr in the cout

To enhance your understanding, consider using cout

While C 11 offers an elegant solution:

template 
std::ostream & operator

This overload allows you to print function pointers of arbitrary arity, displaying information like the function pointer's address and the number of its arguments.

So, there you have it: the enigmatic 1s are not a mere coincidence but a result of implicit type conversion. Understanding this behavior is crucial for avoiding unexpected outcomes and crafting more robust code.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3