问题
Is there any way to check step by step what's going on in let's say template? I mean how it is instantiated step by step and so on?
In book I've mentioned here ,
I found (2 minutes ago) quite interesting example of how binary could be implemented as a metafunction.
template <unsigned long N>
struct binary
{
static unsigned const value
= binary<N/10>::value << 1 // prepend higher bits
| N%10; // to lowest bit
};
template <> // specialization
struct binary<0> // terminates recursion
{
static unsigned const value = 0;
};
and I think it could be quite useful to be able to see step by step what's been done during the instantiation of this template. Thanks for your replies.
回答1:
The best i've seen this far was the research paper on Templight, but i am not aware of any publicized implementation.
You can help yourself much though by using descriptive static (i.e. compile time) assertions - see e.g. Boosts static assert or MPLs asserts. In some cases it can help to provoke a compile error (e.g. by using static asserts) to get a template instantiation trace from the compiler.
Also there is nothing preventing you from a runtime output of meta-function results for testing.
来源:https://stackoverflow.com/questions/2483426/debugging-metaprograms