I was trying to code the O(N) solution to find the square root of a perfect square number using template metaprogramming in C++. Algorithm:
algorithm sqrt(N, start):
if(start*start == N)
return start
else
return sqrt(N, start+1)
i.e:
template<int value, int N>
struct sqrt
{
enum { val = ((value*value == N) ? value : (sqrt<(value+1), N>::val)) };
};
and instantiating sqrt<1, 4> in main().
I'm running into "template instantiation depth exceeds maximum of 900" error, eventhough it has to stop try and instantiate when value is 2?
Am I missing something specific for template metaprogramming? Does it executes both side of the ternary operator, irrespective of the condition? Kindly help me figure this out?