我正在尝试扩展 std::packaged_task
来处理 SEH 异常,但我无法编译它。
以下不编译:
#include <stdio.h>
#include <functional>
#include <future>
#include <windows.h>
template<class RET, class... ARGS>
class SafePackagedTask : public std::packaged_task<RET(ARGS...)>
{
public:
template<class F>
explicit SafePackagedTask(F && f)
: std::packaged_task*(std::forward<F>(f))
{
}
void operator()(ARGS... args)
{
__try
{
std::packaged_task*(std::forward<ARGS>(args));
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
printf("SEH Exception 0x%08lX in SafePackagedTask!\n", GetExceptionCode());
}
}
};
int main()
{
SafePackagedTask<int()> task([] {
//int *a = nullptr; *a = 1; // generate SEH
return 1;
});
std::future<int> fut = task.get_future();
task();
int rc = fut.get();
printf("result: %d\n", rc);
}
错误是:
source_file.cpp(9): error C2091: function returns function
source_file.cpp(37): note: see reference to class template instantiation 'SafePackagedTask<int (void)>' being compiled
source_file.cpp(38): error C2440: 'initializing': cannot convert from 'std::future<_Ret>' to 'std::future<int>'
with
[
_Ret=int (__cdecl *)(void)
]
source_file.cpp(38): note: No constructor could take the source type, or constructor overload resolution was ambiguous
查看 rextester.com .
最佳答案
你正在尝试解决一个已经解决的问题。
SEH 异常可以像常规 C++ 标准异常一样处理,方法是在 VC++ 上设置特定标志(您显然正在使用它)*
转到项目 -> C/C++ -> 代码生成 -> 在“Yes with SEH exceptions”上设置“Enable C++ exceptions”。
现在您可以使用 catch(...)
子句捕获 SEH 异常:
try{
}catch(std::exception& e){}
catch(...) {/*your code here*}
除此之外,处理 SEH 异常的最好方法是一开始就不要创建它们。不要试图读取超出数组边界的内容、检查空指针、在需要时使用智能指针并仔细编写类以使用 RAII 可能会消除代码中 95% 的异常。
*基本上,在 Windows 上,所有 C++ 异常都是 Windows SEH 的子集,并以此实现。
关于C++11 如何扩展类模板,如 std::packaged_task,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35335822/