1

我想要实现的是将 lambda 表达式传递给一个函数,该函数将该传递的表达式用作该函数的格式化boost::xpressive::regex_replace函数。该代码应该使用 VS 2010 在 VC++ 中工作。

如果我有一个功能

void test1(){
    std::string str("foo");
    sregex re(icase("foo"));
    str = regex_replace(str,re,[](const smatch &match){ return "bar";});
}

我可以轻松地将 lambda 表达式作为格式函数传递。它也可以作为函数指针传递:

std::string test2_format(const smatch &match){
    return "bar2";
}

void test2_replace(std::string(*fun)(const smatch &match)){
    std::string str("foo");
    sregex re(icase("foo"));
    str = regex_replace(str,re,fun);
}

void test2(){
    test2_replace(test2_format);
}

但是如何将 lambda 表达式传递给函数呢?如果我打电话给类似的东西

test2_replace([](const smatch &match)->std::string{return "bar3";});

我得到错误

错误 C2664:“test2_replace”:无法将参数 1 从“匿名命名空间”::' 转换为“std::string (__cdecl *)(const boost::xpressive::smatch &)”

此错误也发生在 VS 2013 上。

[编辑] 根据这个链接lambda 到函数指针的转换应该被支持/修复。[/编辑]

你知道我可以尝试什么吗?

4

0 回答 0