调试元程序 [英] Debugging metaprograms

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

问题描述

有没有办法一步一步地检查让我们说模板发生了什么?我的意思是如何逐步实例化等等?



在书中我提到了这里



我发现(2分钟前)相当有趣的例子,说明二进制如何被实现为元功能。 >

 模板< unsigned long N> 
struct binary
{
static unsigned const value
= binary< N / 10> :: value<< 1 //预加高位
| N%10; //到最低位
};

template<> // specialization
struct binary< 0> // terminate recursion
{
static unsigned const value = 0;
};

我认为可以一步一步看到在实例化此模板。
感谢您的回复。

解决方案

我看过的最好的是关于 Templight ,但我不知道有任何公开的实现。



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

也没有任何东西阻止您从元函数结果的运行时输出用于测试。


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.

解决方案

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.

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

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