ManuelAtWork
ManuelAtWork

Reputation: 2458

How to debug-output a pointer-to-member?

For debugging purposes, I want to print the values of template parameters.

For type template parameters, I can use typeid(T).name(), which gives me (more or less) the name of the type T as a string.

Is there a way to get a similar string (e.g. "&Parent::member") for a pointer-to-member template parameter? I cannot use typeid here, because the pointer is not a type.

The debugging string shall contain the name of the member as well as the type name of the parent.

Upvotes: 1

Views: 162

Answers (1)

geza
geza

Reputation: 29962

Variable/member names don't get written into the compiled objects files by default. So this information is lost during the compilation process, you cannot get it. The only exception is if you compile with debug information, but even this case, debug information is not loaded with your exe when you execute it.

I see two possible solutions:

  1. Use an additional template parameter, which is the name of the pointer-to-member. This name must be specified manually (maybe you can create a macro for that)
  2. If you have debug information in the executable, you could try to get this information. This solution is compiler dependent, and maybe needs hacky techniques. For gcc/clang, debug information is usually in DWARF format, which is documented. For MSVC, there is Debug Interface Access SDK and this. Cumbersome solution, maybe not worth the hassle.

Upvotes: 1

Related Questions