lundi 25 janvier 2016

function name in if statement is casted in a strange way

Today, my girlfriend had a bug that got me baffled. I don't know how to properly explain why the observed behavior actually happens, so I am asking for your help here.

Long story short, here is the code (it's small, so I'll leave everything inside so it actually compiles with 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, "WAT???\n");
  }
  return 0;
}

So the question is inside of the if statement. While typeid returns me smth that looks like FbiE (which I think is gcc language for function type) I do not understand why this type is being implicitly casted into bool (just an example, also works with int). Can you give me some pointers on this?

Aucun commentaire:

Enregistrer un commentaire