Reputation: 13
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;
}
Upvotes: 1
Views: 87
Reputation: 373482
I believe the problem is in your template code:
template<int count, function_t** f> class loop_t
{
public:
static inline void exec()
{
(*(*f))();
loop_t< count-1, f>::exec();
}
};
Notice that you instantiate this inner template:
loop_t< count-1, f>::exec();
The problem is that you've never defined a partial specialization of loop_t
that terminates when the loop counter reaches some value (say, zero), and so the compiler just keeps on instantiating more and more versions of loop_t
with lower and lower values of count
until it reaches an internal limit and reports an error. To fix this, you should introduce a partial specialization of loop_t
to halt when the counter hits some value (probably zero):
template<function_t** f> class loop_t<0, f>
{
public:
static inline void exec()
{
// Empty
}
};
Hope this helps!
Upvotes: 6