Reverse stop
Without knowing exactly what you need, I would look at implementing my own stack and avoid recursion entirely. Thus, it becomes trivial to exit the backtrace tree (one "backtrace"), and you can also resume searching to find the next solution by calling the function again, assuming that the state of the user stack is preserved (in static variables). Of course, there is a bit of programming overhead to convert a simple recursive program to a loop, but it's pretty straightforward to do.
a source to share
Why do you want the function to exit immediately? It is dangerous not to backtrack through the stack, as you may have objects that need to be destroyed. Throwing an exception might be triggered by a trick for you and it will clear the stack. Please provide more information on what you are trying to do and we could provide other approaches.
a source to share
If your backtracking algorithm actually recurses deep enough for it to matter, then you shouldn't use recursion because you would be in danger of blowing the stack. Rather than committing some atrocity involving longjmp, you should consider rewriting your algorithm to be an iterative approach with your own heap stack that stores a POD object representing the state. When you find your solution, the state container can be destroyed in one efficient step and a response can be returned.
a source to share
First, please note that you cannot do this for any recursive function. Consider the following code:
int SumNum(int nMax)
{
if (0 == nMax)
return 0;
else
return nMax + SumNum(nMax-1);
}
The actual value is calculated during backtracking.
However, you can rewrite your code like this:
int SumNum(int nMax, int nSum)
{
if (0 == nMax)
return nSum;
else
return SumNum(nMax-1, nSum+nMax);
}
Now you can do the following trick:
int SumNum(int nMax, int nSum)
{
if (0 == nMax)
throw nSum;
else
return SumNum(nMax-1, nSum+nMax);
}
f()
{
int nSum;
try
{
SumNum(100, 0);
}
catch (int _nSum)
{
nSum= _nSum;
}
}
a source to share