我把自己与未来和承诺之间的区别混淆了.
显然,他们有不同的方法和东西,但实际的用例是什么?
是吗?:
我正在使用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)