Stack overflow is caused by B copy constructor invoking default constructor of std::nested_exception which tries to capture current exception. Capturing current exception copies it and since our current exception is B, we call B's copy constructor and cause infinite recursion.
After fix, "throw b" no longer captures current exception (it copies it from B(5) object where it is empty) and so throw_if_nested terminates the test.