如何使用boost::bind从参数中实时提取数据
How to extract data on-fly from the argument with boost::bind
假设你正在传递一个函数给另一个函数:
foo(boost::bind( &CLASS_NAME::OnValueChanged, this, _1));
但问题是_1
类型可以变化,我真的不需要它,只需要它的一个数据。所以我想这样写:
foo(boost::bind( &CLASS_NAME::OnValueChanged, this, _1.GetName()));
因为无论_1
是什么类型,我知道每次都有GetName()
方法,结果类型都是一样的(wstring
)。
怎么做?
另外,在我的例子中,包含GetName
的类型是一个模板
你只需要再绑定一次!
foo(boost::bind( &CLASS_NAME::OnValueChanged, this, boost::bind(GetNameFunctor(), _1));
GetNameFunctor()的通用实现如下所示
struct GetNameFunctor {
typedef std::string result_type;
template <typename T> std::string operator()(T const& o) const {
return o.GetName();
}
};
如果你想要更多的控制/支持,你可以看看
- http://www.boost.org/doc/libs/1_55_0/doc/html/boost_typeerasure.html
相关文章:
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- 如何从 std::atomic 中提取指针 T<T>?
- 为什么istream不支持右值提取
- 如何设置一个范围来提取我想要获得的信息
- 从数据库实时显示QT c++中的数据
- 视觉工作室项目.提取源文件夹名称
- C++17 - 使用自定义分配器的节点提取/重新插入 - 适用于 clang++/libc++,但不适用于 libstd
- 在 Windows 8/10 技术中完全实时的屏幕捕获,没有延迟
- 从字符串中提取整数并形成一个数组
- C ++中的StringStream有助于使用向量从字符串中提取逗号分隔的整数,而不是空格分隔的整数,为什么?
- asn1c 不会从 asn.1 模块中提取八位字节字符串的默认值
- 从 std::vector<无符号字符>切片中提取 int?
- 在C++中使用重载提取运算符时出现问题
- 在 Qt(C++) 中使用 QProcess 解压缩 - 提取目录问题
- C++(.cpp文件和.h文件)拆分代码并添加一个函数,提取 - 这很容易吗?
- 有没有办法使用 c++ 实时阅读文本?
- 在本地网络中通过OpenCV(C++)实时流式传输图像
- 将相机数据从服务器实时流式传输到客户端
- 如何使用boost::bind从参数中实时提取数据
- 使用opencv mogdetector和c++从实时摄像机馈电中提取背景