Debug visualizers for C++ templates

Posted by on in Tools

A few weeks ago, I wrote about how RAD Studio 10.2.1 supports debug vizualizers for Delphi generics:

Debug visualisers are a type of IDE plugin that allows you to change the display of a variable in the various debug windows (Local Variables, Watches, Inspector, and Evaluate/Modify.) For example, the TDateTime visualiser takes the double value that represents time and instead displays that time converted to a string. You register a debug visualiser for a type, so all variables of that type, and optionally descendants of that type, go through your visualiser.

In previous versions, as with generics, there was no way to register a visualizer for "MyTemplate<T, U>".  The visualizer would never fire because there would be no type in the compiled app named "MyTemplate<T, U>" - it would be a specific instantiation, such as MyTemplate<int, std::string>.

In 10.2.1, we have introduced support for registering a visualizer for C++ templates.  In fact, it works exactly the same as Delphi: the only trick to be aware of is that sometimes a C++ template has more parameters than you may be aware of.  This is important when implementing the visualizer's GetSupportedType() method which returns the type name.

For example, you may want to register a visualizer for a vector, and use the typename "std::vector<T>".  When the visualizer is installed, you won't see it have any effect and will realize it's not being called for vectors.  You actually need to return a typename with two generic parameters (the name of each doesn't matter), such as "std::vector<T, Allocator>".  Similarly, a std::map is a template with not only key and value template params, but comparison and allocator params too, so the correct type string is something like "std::map<K, V, Compare, Allocator>" (again, the exact names of the parameters doesn't matter.)

Unlike visualizers for a normal type, the visualizer is called only for an instantiation of the type with the type string specified, not descendants of the type.  If you need to alter display for descendants of a generic, you will need to register for those descendants too.

Finally, here is a code snippet demonstrating a template visualizer

Templates are widely used in C++ and we hope being able to register a visualizer for them will be useful for you!

Gold User, No rank,
C++ Product Manager, looking after C++Builder as well as the IDE.


Check out more tips and tricks in this development video: