用 std 实现替换 boost

SPl*_*ten 4 c++ boost std c++11

替换它的正确方法是什么:

std::ostringstream buf;
std::for_each(bd.begin(), bd.end(), buf << boost::lambda::constant("&nbsp;") << boost::lambda::_1);
Run Code Online (Sandbox Code Playgroud)

使用不使用 boost 的实现?这是我尝试过的:

std::string backspace("&nbps;");
std::ostringstream buf;        
std::for_each(bd.begin(), bd.end(), buf << backspace << std::placeholders::_1);
Run Code Online (Sandbox Code Playgroud)

第二个 '<<' 用红色下划线表示,我收到错误消息:

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::_Ph<1>' (or there is no acceptable conversion)
Run Code Online (Sandbox Code Playgroud)

Que*_*tin 7

boost::lambda是一个奇妙的怪物,将 lambdas 反向移植到 C++03。相当于您的代码是:

std::ostringstream buf;
std::for_each(bd.begin(), bd.end(), [&](auto const &v) { buf << "&nbsp;" << v; });
Run Code Online (Sandbox Code Playgroud)

... 甚至:

std::ostringstream buf;
for(auto const &v : bd)
    buf << "&nbsp;" << v;
Run Code Online (Sandbox Code Playgroud)