-4

I was playing with typeid header-file in cpp. I passed a function name to the typeid function. When I did std::cout << ti.name() << std::endl I got FficE as output. I could figure out(kind of) everything except E. Can you guys explain how this works?

Here is the code:

#include <iostream>
#include <typeinfo>

float myFun(int num1, char num2){
    return 25.34;
}
int main(void){
    const std::type_info& ti2 = typeid(myFun); // O/P: FficE
    /*
     F: Function
     f: float (return type)
     i: integer (parameter 1)
     c: character (parameter 2)
     E: ?
     */

    std::cout << ti2.name() << std::endl;
    return 0;
}

I tried to read the official documentation but couldn't get much out of it. Also, I couldn't find a similar question asked here as well.

I'm using g++ as my compiler

  • Also when I pass a method name(function declared inside a class) it only gives out the return type of the function – boredAndDesperate25 Mar 11 '23 at 17:43
  • 2
    Which compiler? There is no standard for name mangling in c++ – Alan Birtles Mar 11 '23 at 17:45
  • Does this solve your problem: https://stackoverflow.com/questions/3000138/can-typeid-be-used-to-pass-a-function – Notiq Mar 11 '23 at 17:45
  • @Notiq that question would apply to `typeid(myFun())` not `typeid(myFun)` – Alan Birtles Mar 11 '23 at 17:46
  • 1
    The name produced by `typeid` is implementation-defined. There is no general rule for it. You need to tell us what compiler you are using. It looks like part of a mangled name, but I wouldn't know according to which ABI. – user17732522 Mar 11 '23 at 17:51
  • 1
    It looks like `E` is just the end of a function type name. `FE`. Without it you could have ambiguous names. e.g. `FvPFiii` could be the "type name" of either `void f(int (*fp)(int), int)` or `void f(int (*fp)(int, int))`. But with an ending character those would be `FvPFiiEiE` and `FvPFiiiEE`. – Kevin Mar 11 '23 at 17:54
  • looks like you're using some form of Itanium based compiler, see https://itanium-cxx-abi.github.io/cxx-abi/abi-mangling.html. `F` is a function, `f` is the float return type, `ic` are the `int` and `char` arguments, `E` is the end of the argument list – Alan Birtles Mar 11 '23 at 17:59

1 Answers1

2

The result of std::type_info::name is implementation defined.

It looks like you're using some form of Itanium based compiler, see https://itanium-cxx-abi.github.io/cxx-abi/abi-mangling.html.

F is a function, f is the float return type, ic are the int and char arguments, E is the end of the argument list.

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60