Funciones de impresión en cpp [duplicado]

Oct 25 2020

¿Por qué cada vez que imprimo una función en cpp, imprime 1. ie

#include <iostream>
using namespace std;
void f1(){
   cout<<"called function f";
}
int f2(){
   cout<<"called f2";
   return 1;
}
int main(){
   cout << f2 <<endl; // prints 1
   cout << f1 <<endl; // still prints 1
}

Me pregunto por qué imprime 1.

Gracias por tu ayuda

Respuestas

1 MikeCAT Oct 25 2020 at 20:20

Parece que está juzgando si las funciones (convertidas en punteros a funciones) no son nulas, regresando truee imprimiendo truecomo 1. Esto se puede confirmar agregando cout << std::boolalpha;y viendo que la salida cambia a true.

#include <iostream>
using namespace std;
void f1(){
   cout<<"called function f";
}
int f2(){
   cout<<"called f2";
   return 1;
}
int main(){
   cout << std::boolalpha;
   cout << f2 <<endl; // prints true
   cout << f1 <<endl; // also prints true
}