views:

119

answers:

1

Hi, 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.

A: 

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.

Georg Fritzsche
Yes I think runtime output will suite me well. Thanks
There is nothing we can do
Having said that now I've encounter a problem with output. I've tried something like so:cout << N;but I'm getting errors. Help.
There is nothing we can do
You can't just throw output statements in meta-functions, but you can output values from concrete instantiations of meta-functions, e.g. `std::cout << binary<101>::value`.
Georg Fritzsche