c++ - boost::function & boost::lambda 再次

标签 c++ boost

后续跟进:Using * Width & Precision Specifiers With boost::format

我正在尝试使用 boost::function 创建一个函数,该函数使用 lambda 来格式化带有 boost::format 的字符串。最终我想要实现的是对具有格式的字符串使用宽度和精度说明符。 boost::format 不支持使用 * 宽度和精度说明符,如 in the docs 所示:

Width or precision set to asterisk (*) are used by printf to read this field from an argument. e.g. printf("%1$d:%2$.*3$d:%4$.*3$d\n", hour, min, precision, sec); This class does not support this mechanism for now. so such precision or width fields are quietly ignored by the parsing.

所以我正在尝试寻找其他方法来实现相同的目标。

这是我目前所拥有的,但无法正常工作:

#include <string>
#include <boost\function.hpp>
#include <boost\lambda\lambda.hpp>
#include <iostream>
#include <boost\format.hpp>
#include <iomanip>
#include <boost\bind.hpp>

int main()
{
 using namespace boost::lambda;
 using namespace std;

 boost::function<std::string(int, std::string)> f =
  (boost::format("%s") % boost::io::group(setw(_1*2), setprecision(_2*2), _3)).str();

 std::string s = (boost::format("%s") % f(15, "Hello")).str();

    return 0;
}

这会产生许多编译器错误:

1>------ Build started: Project: hacks, Configuration: Debug x64 ------
1>Compiling...
1>main.cpp
1>.\main.cpp(15) : error C2872: '_1' : ambiguous symbol
1>        could be 'D:\Program Files (x86)\boost\boost_1_42\boost/lambda/core.hpp(69) : boost::lambda::placeholder1_type &boost::lambda::`anonymous-namespace'::_1'
1>        or       'D:\Program Files (x86)\boost\boost_1_42\boost/bind/placeholders.hpp(43) : boost::arg<I> `anonymous-namespace'::_1'
1>        with
1>        [
1>            I=1
1>        ]
1>.\main.cpp(15) : error C2664: 'std::setw' : cannot convert parameter 1 from 'boost::lambda::placeholder1_type' to 'std::streamsize'
1>        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>.\main.cpp(15) : error C2872: '_2' : ambiguous symbol
1>        could be 'D:\Program Files (x86)\boost\boost_1_42\boost/lambda/core.hpp(70) : boost::lambda::placeholder2_type &boost::lambda::`anonymous-namespace'::_2'
1>        or       'D:\Program Files (x86)\boost\boost_1_42\boost/bind/placeholders.hpp(44) : boost::arg<I> `anonymous-namespace'::_2'
1>        with
1>        [
1>            I=2
1>        ]
1>.\main.cpp(15) : error C2664: 'std::setprecision' : cannot convert parameter 1 from 'boost::lambda::placeholder2_type' to 'std::streamsize'
1>        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>.\main.cpp(15) : error C2872: '_3' : ambiguous symbol
1>        could be 'D:\Program Files (x86)\boost\boost_1_42\boost/lambda/core.hpp(71) : boost::lambda::placeholder3_type &boost::lambda::`anonymous-namespace'::_3'
1>        or       'D:\Program Files (x86)\boost\boost_1_42\boost/bind/placeholders.hpp(45) : boost::arg<I> `anonymous-namespace'::_3'
1>        with
1>        [
1>            I=3
1>        ]
1>.\main.cpp(15) : error C2660: 'boost::io::group' : function does not take 3 arguments
1>.\main.cpp(15) : error C2228: left of '.str' must have class/struct/union
1>Build log was saved at "file://c:\Users\john\Documents\Visual Studio 2005\Projects\hacks\x64\Debug\BuildLog.htm"
1>hacks - 7 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

我可能缺乏对 boost 的 lambda 和函数的基本理解。我怎样才能让它发挥作用?

最佳答案

我认为,对于这种情况,您会希望使用 boost.bind 而不是 boost.lambda。部分问题在于 boost::io::group 是一个函数模板,它接受并返回可变数量的对象,因此很难为 function<> 声明创建适当的签名。我会创建一个带有简单签名的字符串格式化函数,然后使用 boost.bind 从中创建一个特定的格式化仿函数。即

#include <string>
#include <iomanip>
#include <boost/function.hpp>
#include <boost/format.hpp>
#include <boost/bind.hpp>

using namespace boost;
using namespace std;

string fmt_str(const string& s, int w, int p)
{
    return (format("%s") % io::group(setw(w), setprecision(p), s)).str();
}

int main()
{
    function<string (int, string)> f = bind(fmt_str, _2, _1, _1);
    string s = f(15, "Hello");
    return 0;
}

关于c++ - boost::function & boost::lambda 再次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2981724/

相关文章:

c++ - 代码块、MinGW、Boost 和静态链接问题

c++ - 如何在boost中使用digamma函数

c++ - 将CMakeLists.txt与带有不同标志的多个目标一起使用

c++ - 将元素添加到二维数组 C++

c++ - MSVS 2015 : vector<bool> has no 'data' member

c++ - 防止 clang tidy 报告关于 Boost 测试 header 的警告

c++ - 如何判断哪一行代码创建了新线程(gdb)?

C++:标签/特征模板:确保标签只绑定(bind)一次

c++ - 如何将库添加到我的 NetBeans C++ 项目?

c++ - 如何将指针传递给模板类的成员函数?