【问题标题】:Replacing boost with std implementation用 std 实现替换 boost
【发布时间】:2020-12-25 16:06:04
【问题描述】:

替换这个的正确方法是什么:

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

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

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

第二个“

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

【问题讨论】:

    标签: c++ c++11 boost std


    【解决方案1】:

    boost::lambda 是一个了不起的怪物,它将 lambdas 向后移植到 C++03。相当于您的代码是:

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

    ...甚至:

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

    【讨论】:

    • 尝试了第一个建议,得到:error C3533: 'const auto &': a parameter cannot have a type that contains 'auto'
    • @SPlatten 在 C++14 之前,auto lambda 不是一个东西。只需使用 bd 中存储的具体类型即可。
    • typename decltype(bd)::const_reference v
    猜你喜欢
    • 2021-12-19
    • 1970-01-01
    • 1970-01-01
    • 2016-04-23
    • 2013-08-23
    • 1970-01-01
    • 1970-01-01
    • 2015-09-03
    • 1970-01-01
    相关资源
    最近更新 更多