When doing metaprogramming using C++ templates, is there a method that can be used, sort of like a debugger, to step through how the templates are being instantiated and complied? It seems right now, when creating a complicated network of templates, there really isn't a very good way of debugging them other than looking at the complier error messages to see how the templates are being instantiated (if there are any compiler errors), and the attempt to work backwards from the error messages if something unexpected is being generated. I'm not really sure if what I'm looking for even exists, as it would have to be something that is done at compile time, but basically it would be a method, sort of like stepping through code and examining the stack frame in gdb
at runtime, where the compiler could be stopped and the environment examined for the sequence by which a template or set of nested templates is being instantiated.
For instance, let's say I created some simple code like the following:
template<typename T, typename R = void>
struct int_return_type {};
template<typename R>
struct int_return_type<int, R>
{
typedef R type;
};
template<typename T, typename R = void>
struct float_return_type {};
template<typename R>
struct float_return_type<float, R>
{
typedef R type;
};
template<typename T>
typename int_return_type<T>::type test()
{
cout << "T type is int" << endl;
}
template<typename T>
typename float_return_type<T>::type test()
{
cout << "T type is float" << endl;
}
int main()
{
test<int>();
test<float>();
return 0;
}
I know this is relatively easy code to follow, but templates can get quite a bit more involved, especially when doing metaprogramming, recursion, etc. I understand that the complier will issue error messages that can be used to deduce how templates are being instantiated, but I'm also wondering what can be done when the actual template code is correct in a syntactic sense, but the runtime results are still incorrect. It would be nice for instance to have a method to stop the compiler and see what test
, as well as int_return_type
and float_return_type
, was being instantiated with, or what instantiations were failing.
Are the only options available right now for debugging templates with this level of granularity 1) the compiler error messages when the code is incorrect, and 2) a combination of disassemblers and debuggers to see what instantiated code was generated if the run-time results are incorrect? Or are there some other utilities out there that help with "watching" how templates are instantiated, and see/inspect what code is generated by the compiler to investigate and debug template errors?
See Question&Answers more detail:os