I tried this and got the output as:
void
Please explain the following Code:
#include <cstdio>
#include <typeinfo>
using namespace std ;
void foo()
{ }
int main(void)
{
printf("%s",
typeid(foo()).name());// Please notice this line, is it same as typeid( ).name() ?
return 0;
}
AFAIK:
The typeid operator allows the type of an object to be determined at run time.
So, does this sample code tell us that a function that returns void is of **type void**.
I mean a function is a method and has no type. Correct?
typeiddoes not work with objects. It works with expressions.typeidreturns the type of the expression you supply to it as an argument. The expression can refer to an object, or to something that is not an object. You supplied expressionfoo()as an argument. This expression has typevoid. So, you got a result that refers to typevoid.void, BTW, is not an object type.Function do have types. If you want to apply
typeidto the function itself, the syntax would betypeid(foo). The function-to-pointer conversion is not applied to the argument oftypeid, which means that you should get a result that refers to function type itself. Meanwhile,typeid(&foo)will give you a function pointer type id, which is different fromtypeid(foo).