C++11 如何扩展类模板,如 std::packaged_task

标签 c++ c++11 visual-studio-2015

我正在尝试扩展 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/

相关文章:

在 C++ 应用程序后面运行的 C++ 计时器

c# - Visual Studio 2015 : error building solution

azure - 在 VS2015 中发布到 azure,消息为 : The 'prepublish' script failed with status code 1

c++ - Visual Studio 2015 添加类,错误 : "object already exists"

c++ - 使用字符串在 C++ 中设置控制台标题

c++ - boost interprocess file_lock不适用于多个进程

C++ - 为什么可以在复制构造函数中直接访问传递的对象的私有(private)变量?

c++ - 有没有办法知道当前程序消耗的总内存?

c++ - Microsoft 的 DirectX 11 教程在释放变量时崩溃

c++ - 检查文件名是否已存在于文件夹中?