调试元程序 [英] Debugging metaprograms

查看:99
本文介绍了调试元程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法来逐步检查我们说模板中发生了什么吗?我的意思是如何逐步实例化等等。

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?

在书中,我提到了这里

我发现(2分钟前)相当有趣的例子二进制可以实现为一个元函数。

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.

推荐答案

我看过的最好的是研究论文 Templight ,但我不知道任何公开的实施。

The best i've seen this far was the research paper on Templight, but i am not aware of any publicized implementation.

你可以通过使用描述性静态(即编译时)断言帮助自己 - 参见例如提升静态断言 MPL声明。在某些情况下,它可以帮助引发编译错误(例如通过使用静态断言)从编译器获取模板实例化跟踪。

也没有什么阻止您从元函数结果的运行时输出用于测试。

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.

这篇关于调试元程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆