I am unable to compile the below program in VS2010. Keeps compiling endless and got into heap not available. Any help is much appreciated.
#include <iostream>
class function_t
{
public:
virtual void operator ()()=0;
};
class greet_t : public function_t
{
public:
virtual void operator()(){ std::cout << "hello world" << std::endl;}
};
template<int count, function_t** f> class loop_t
{
public:
static inline void exec()
{
(*(*f))();
loop_t< count-1, f>::exec();
}
};
function_t* f;
int _tmain(int argc, _TCHAR* argv[])
{
f = new greet_t();
loop_t<1, &f>::exec();
return 0;
}
I believe the problem is in your template code:
Notice that you instantiate this inner template:
The problem is that you’ve never defined a partial specialization of
loop_tthat terminates when the loop counter reaches some value (say, zero), and so the compiler just keeps on instantiating more and more versions ofloop_twith lower and lower values ofcountuntil it reaches an internal limit and reports an error. To fix this, you should introduce a partial specialization ofloop_tto halt when the counter hits some value (probably zero):Hope this helps!