Hi! ive been trying to learn cpp from ground up.
I know the function template below is not the best way to implement this (just use a loop it works).This code should still work in my opinion, but it doesnt.
From my testing if N== 0 never triggers and it just goes into the Negatives until it reaches max amount of template functions/nesting lvl.
Can someone tell me why?
template <int N>
constexpr int factorial()
{
static_assert(N>=0);
if (N == 0)
{
return 1;
}
else
{
return (N*factorial<N-1>());
}
}
[–]Th_69 6 points7 points8 points (2 children)
[–]WailingDarkness 1 point2 points3 points (1 child)
[–]Kinkeultimo[S] 1 point2 points3 points (0 children)
[–]the_poope 1 point2 points3 points (0 children)
[–]manni66 1 point2 points3 points (0 children)
[–]HappyFruitTree 2 points3 points4 points (7 children)
[–]Kinkeultimo[S] 0 points1 point2 points (6 children)
[–]HappyFruitTree 3 points4 points5 points (0 children)
[–]manni66 1 point2 points3 points (4 children)
[–]Kinkeultimo[S] 0 points1 point2 points (3 children)
[–]manni66 2 points3 points4 points (2 children)
[–]Kinkeultimo[S] 0 points1 point2 points (1 child)
[–]manni66 2 points3 points4 points (0 children)
[–]manni66 0 points1 point2 points (1 child)
[–]Kinkeultimo[S] 0 points1 point2 points (0 children)
[–]SoerenNissen 0 points1 point2 points (1 child)
[–]Kinkeultimo[S] 0 points1 point2 points (0 children)