我有一个函数,它使用boost :: bind将函数求值移到基于this question的try / catch包装器中。
问题是,boost::bind
似乎不适用于SEH - 更糟糕的是,它返回一个垃圾值,这正是我不想要的。我在某个地方搞砸了吗?更奇怪的是,切换/EHsc
或/EHa
实际上对程序的行为似乎并不重要。你得到的输出是:
Calling
-2147483648
Done.
Press any key to continue . . .
我如何开始弄清楚出了什么问题?
#pragma once
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <iostream>
#include <windows.h>
#include <eh.h>
using boost::function;
using boost::bind;
int potato(int q)
{
double r = 0;
return q / r;
}
template<typename T>
T exception_wrapper(boost::function<T()> func)
{
try
{
std::cout << "Calling" << std::endl;
return func();
std::cout << "After Call" << std::endl;
}
catch (...) {
std::cout << "Ex Caught" << std::endl;
return 45;
}
}
void trans_func(unsigned int u, EXCEPTION_POINTERS* pExp)
{
throw std::runtime_error("huh");
}
int main()
{
int result = exception_wrapper<int>(bind(potato, 123));
std::cout << result << std::endl;
std::cout << "Done." << std::endl;
}