7

With this code (valid C++11):

#include <stdio.h>
#include <typeinfo>

bool my_awesome_func(int param) {
  return (param > 1);
}

int main(int argc, char const *argv[]) {
  fprintf(stderr, "type of my_awesome_func: %s\n", 
          typeid(my_awesome_func).name());
  if (my_awesome_func) {
    fprintf(stderr, "WHAT???\n");
  }
  return 0;
}

The question is inside of the if statement. While typeid returns me something that looks like FbiE (which I think is gcc language for function type) I do not understand why this type is being implicitly converted into bool (just an example, also works with int).

Why does the if statement compile and evaluate true?

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
niosus
  • 738
  • 8
  • 22
  • 8
    Function name decays to a pointer to that function, and pointers are implicitly convertible to `bool`, yielding `false` if it's null and `true` otherwise. – T.C. Jan 25 '16 at 22:34
  • @T.C. thanks! As soon as I know that it converts to pointers I understand what happens then. – niosus Jan 25 '16 at 22:40
  • 5
    Giving you some *pointers*, heh. – Quentin Jan 25 '16 at 22:43
  • 1
    Huh, I'm surprised I did not find a good duplicate for this, closest I got was [that one](https://stackoverflow.com/questions/12152167/why-is-using-the-function-name-as-a-function-pointer-equivalent-to-applying-the). – Baum mit Augen Jan 25 '16 at 23:03
  • @BaummitAugen, you don't say, I did try all my fantasy to search for something similar :) Although, I have to admit that I failed to find the one pointed out by you either. – niosus Jan 25 '16 at 23:29
  • @niosus It is certainly easier to look for a dupe when you already know the answer, it is not surprising you did not find the one I linked even with good research. Anyways, I just felt like I saw this before, but apparently I was wrong or it is really hard to find. All in all a nice question, you have my +1. :) – Baum mit Augen Jan 25 '16 at 23:45

1 Answers1

10

There is no casting in your code. A cast is an explicit conversion. I assume you are asking: What does implicit conversion of a function to bool do?

The answer to that is: the function is converted to a function pointer. Then the function pointer is converted to bool via implicit conversion. That conversion is defined as yielding:

  • false for a null function pointer
  • true for any other function pointer

So in your code, the body of if (my_awesome_func) is always entered. (Converting an actual function to a function pointer never yields a null pointer).

M.M
  • 138,810
  • 21
  • 208
  • 365
  • Thanks! You are right, I surely meant the implicit conversion, but could not remember the proper word (I sometimes suck in English). I understand the dynamics after the function is converted to a function pointer, but I would like to read more on that. I understand that it does not perform the conversion in the `typeid` call. I know that is specific to `typeid` itself, but maybe there are other guys with similar behavior – niosus Jan 25 '16 at 22:42