Debugging metaprograms

偶尔善良 提交于 2019-12-23 09:14:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!