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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux