This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: result type of an operation betweem signed and unsigned, c++


Hi Dima,

> 2) Is there some way to convert a type of an expression into
>     _readable_ string  ("typeof"), to debug such things ?

- - - - - - - - - - - - - - - - - - - - - - >8 - - -

#include <iostream>
#include <typeinfo>

int main()
{
  int i(0);
  unsigned u(1);
  bool isPos( i-u > 0 );
  int res(i-u);

  std::cout << i-u << ' ' << isPos << ' ' << res << '\n';
  std::cout << typeid(i-u).name()
    << ' ' << typeid(isPos).name()
    << ' ' << typeid(res).name() << '\n';
}

- - - - - - - - - - - - - - - - - - - - - - >8 - - -

Note: the name generated in the typeinfo varies by compiler.  Could,
technically (but that would be silly) vary from run-to-run of the binary.
Some may use single characters.  Others more human-friendly names.  Others
could use UUID.

If you *really* need human (or developer) readable names, you can make a
std::map<std::string, std::string> of the ones you care about via...

MySpiffyTypeMap[typeid(unsigned)] = "unsigned int";
MySpiffyTypeMap[typeid(int)] = "signed int";
MySpiffyTypeMap[typeid(bool)] = "bool";

HTH,
--Eljay


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]