小编Joh*_*oty的帖子

在C++ 11中,如何根据返回类型对一个函数对象进行专门化?

我在C++ 11中有一个包装函数,设计用于lambdas,如下所示:

template<typename Func>
int WrapExceptions(Func&& f)
{
  try
  {
    return f();
  }
  catch(std::exception)
  {
    return -1;
  }
}
Run Code Online (Sandbox Code Playgroud)

我可以这样称呼它:

int rc = WrapExceptions([&]{
  DoSomething();
  return 0;
});
assert(rc == 0);
Run Code Online (Sandbox Code Playgroud)

生活还可以.但是,我想要做的是重载或专门化包装函数,这样当内部函数返回void时,外部函数返回默认值0,例如:

int rc = WrapExceptions([&]{
  DoSomething();
});
assert(rc == 0);
Run Code Online (Sandbox Code Playgroud)

我可以在C++ 11中实际执行此操作吗?我不能为我的生活思考如何.

c++ templates function-templates c++11

5
推荐指数
1
解决办法
380
查看次数

标签 统计

c++ ×1

c++11 ×1

function-templates ×1

templates ×1