开发者

Debugging metaprograms

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
   }开发者_如何学JAVA;

   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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜