相关疑难解决方法(0)

期货与承诺

我把自己与未来和承诺之间的区别混淆了.

显然,他们有不同的方法和东西,但实际的用例是什么?

是吗?:

  • 当我管理一些异步任务时,我使用future来"获取未来"的价值
  • 当我是异步任务时,我使用promise作为返回类型,以允许用户从我的承诺中获得未来

c++ future promise c++11

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

如何在c ++中创建一个干净的级联if结构?

我正在使用boost的正则表达式库,我发现确定是否找到了命名匹配然后使用该信息有点烦人.要检测命名匹配,我想这样做:

typedef boost::match_result<string::const_iterator> matches_t;
typedef matches_t::const_reference match_t;
boost::regex re("(?:(?<type1>aaaa)|(?<type2>bbbb)" /*...*/ "|(?<typeN>abcdefg)");
string str(SOME_STRING);
matches_t what;
boost::match_flag_type flags = boost::match_default;

if(regex_search(str.cbegin(), str.cend(), what, re, flags))
{
  if((match_t type1 = what["type1"]).matched)
  {
     // do stuff with type1
  }
  else if((match_t type2 = what["type2"]).matched)
  {
     // do stuff with type2
  }
  // ...
  else if((match_t typeN = what["typeN"]).matched)
  {
     // do stuff with typeN
  }
}
Run Code Online (Sandbox Code Playgroud)

如果这样可行,那就太好了.范围将限制在if的主体,内存可以有效使用,看起来相当干净.遗憾的是,它无法正常工作,因为您无法在列表中定义变量.:(

这可能是一种可能性:

if(regex_search(str.cbegin(), str.cend(), what, re, flags))
{
  match_t found = what["type1"];
  if(found.matched)
  {
     // do stuff …
Run Code Online (Sandbox Code Playgroud)

c++ coding-style

8
推荐指数
1
解决办法
271
查看次数

标签 统计

c++ ×2

c++11 ×1

coding-style ×1

future ×1

promise ×1