Debugging C++ template metaprograms
Structure
• Template metaprogramming (TMP)
• Debugging– In general– TMP
• Templight framework– Idea– implementation
• Results
Template metaprogramming
• C++ generics are Turing complete, we can implement algorithms that run in compile time
• Factorial example
Comparison
• Run time vs. compile time
<table>
TMP ontology 1
• Ill-formed and well-formed programswell-formed = according to language rules
TMP ontology 2
Debugging in general
Debugging TMP
Today’s possibilities
• Compiler support (preferred long term solution)
• Language elementsSince we don’t have compiler support, use the existing language elements and the analogue between run time and metaprograms
If we had a trace file?
• We can debug a program a posteriori if we have a trace of the run
• Therefore if we had such a trace file for template metaprograms then we would be able to debug them
Warnings are talkative
• Today’s compilers usually display warning messages that contain essential contextual information
• Artificially generate such warnings
Portable warning
• Consider the following line:…
Build outputsG++ Watcom
MSVC 7.1, 8.0 Comeau
Etc.
Step #1: preprocessing
• Use the existing preprocessor to obtain a single source file
Step #2: instrumentation
• Insert our specially designed warnings to the interested positions
Step #3: Build -> warnings
• Feed the compiler with this modified source and save the warnings
Step #4: warnings -> trace file
• Gather the information from the warnings and create a trace file for further use
Results: TMP debugger IDE
• How it looks like in real
Ideal compiler support
• Special output to migrate these hacks into the compiler
Limitations
• Different levels (no output, only partial info)
• Simple pattern matching is not strong enough, ambiguity
Related work
• Profiling
• TMP book efficieny measurements
• Tracer
• Mpl::print
• archetype
Future directions
• Eliminate limitations
• Profiling
• IDE integration
• Compiler support
Conclusion
• This is a prototype that works with limitations
• With compiler support useful tools could be easily created
Contact
• Home page