3

This is the same question as Is it possible to print a variable's type in standard C++? but I don't want RTTI. I'm writing code with expression templates (e.g. Eigen), which means the types of my variables can be really involved and that I don't know the actual types. However, the compiler knows the types and can tell me when something goes wrong:

error: ‘const struct Eigen::EigenBase<Eigen::Matrix<double, 1, 1, 0, 1, 1> >’ ...

Is there any way I can convert a variable name to a string with the (static) type name so that I could debug the program without breaking it? E.g.

int a;
M b;
cout << TYPEOF(a) << endl << TYPEOF(b) << endl;

would print

int
const struct Eigen::EigenBase<Eigen::Matrix<double, 1, 1, 0, 1, 1> >’
Community
  • 1
  • 1
Mankka
  • 521
  • 3
  • 12
  • 2
    Related (possibly duplicate): http://stackoverflow.com/questions/8001207/compile-time-typeid-without-rtti-with-gcc – John Bartholomew Dec 04 '12 at 12:06
  • @John Not really duplicate: this question is more of a follow-up on the other question. – Konrad Rudolph Dec 04 '12 at 12:07
  • @KonradRudolph: Please check again what question I linked to. – John Bartholomew Dec 04 '12 at 12:07
  • @John I did. Look at the answers. In fact, the accepted answer directly says that you need some Macro fluff to solve this – which leads us here. – Konrad Rudolph Dec 04 '12 at 12:09
  • @KonradRudolph: I'm sorry, I don't understand what part of this question is not covered by a combination of the one linked by the OP and the question I linked to? – John Bartholomew Dec 04 '12 at 12:09
  • @John Well, how do you do it then? – Konrad Rudolph Dec 04 '12 at 12:14
  • @KonradRudolph: I don't, but if I did, I would either accept that I have to use the type-info system (as described in the question OP linked to), or I would use something like the `REFLECTION_REGISTER_TYPE` macro that is described in the question I linked to. – John Bartholomew Dec 04 '12 at 12:17
  • @John My apologies, I indeed overlooked that addition to the *question*. Yes, that indeed answers this one. – Konrad Rudolph Dec 04 '12 at 12:17
  • @KonradRudolph: No problem. And I apologise for dismissing your original comment. – John Bartholomew Dec 04 '12 at 12:18
  • @JohnBartholomew: Since I'm debugging, I accept that I have to use the type-info system. But, the answer http://stackoverflow.com/a/8001243/1693829 basically says that the compiler knows the static type but I have no way of stringifying it. – Mankka Dec 04 '12 at 12:47
  • @Mankka: You do have a way of stringifying it, using type-info, as described in answers to the question you linked to. It's unfortunate that that way is tied to the language's RTTI system, but as noted in ecatmur's answer below, there isn't necessarily any runtime overhead for this functionality. – John Bartholomew Dec 04 '12 at 13:08

3 Answers3

3

typeid can be applied to a type (5.2.8p4):

std::cout << typeid(int).name() << '\n'
  << typeid(M).name() << '\n';

This doesn't involve any run-time overhead.

ecatmur
  • 152,476
  • 27
  • 293
  • 366
3

std::cout << typeid(int).name() << '\n' << typeid(M).name() << '\n';

Edward
  • 31
  • 2
1

I marked https://stackoverflow.com/a/13703184/1693829 as the answer, thank you! This is a wrapper that I wrote:

#include <cxxabi.h>
#include <string>

template <typename T>
std::string t2s(T tt) {
  char *name;
  int status;
  name = abi::__cxa_demangle(typeid(tt).name(), 0, 0, &status);
  std::string namestring(name);
  free(name);
  return namestring;
}
Community
  • 1
  • 1
Mankka
  • 521
  • 3
  • 12