Replacing boost with std implementation

南楼画角 提交于 2021-02-09 02:46:13

问题


What is the correct way to replace this:

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

With an implementation that doesn't use boost? This is what I've tried:

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

The second '<<' is underlined in red and I get the error message:

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

回答1:


boost::lambda is a marvellous monstrosity that kind of backported lambdas to C++03. The equivalent to your code is:

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

... or even:

std::ostringstream buf;
for(auto const &v : bd)
    buf << "&nbsp;" << v;


来源:https://stackoverflow.com/questions/63774109/replacing-boost-with-std-implementation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!