关于Boost库中boost::function和boost::bind使用的问题

IterZebra 2012-04-27 12:41:24
大家好,我想请教一个问题。

class ABC{
static void transfer_configured_vtn_to_vtn_thread();
}

现在如下使用:
boost::function<void(const int32_t, const int32_t, const uint64_t)>
f= boost::bind(&ABC::transfer_configured_vtn_to_vtn_thread);

这样为什么是可以的呢?
boost::function<void(const int32_t, const int32_t, const uint64_t)>
f 是返回void,以const int32_t, const int32_t, const uint64_t为参数的函数对象。
而boost::bind(&ABC::transfer_configured_vtn_to_vtn_thread)是把
static void transfer_configured_vtn_to_vtn_thread()绑定后的函数对象,是没有参数的啊。
为什么可以赋值呢?

望不吝指教。
...全文
189 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
IterZebra 2012-04-27
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]

我觉得你的理解没错。
[/Quote]

哦。谢谢哈。加我QQ 642829312,以后有问题问你。
Jim_King_2000 2012-04-27
  • 打赏
  • 举报
回复
我觉得你的理解没错。
IterZebra 2012-04-27
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

把void (*)()强制转换成了void (*)(const int32_t, const int32_t, const uint64_t)。
[/Quote]

也就是说,是函数指针类型的强制转换。函数指针本质上只是指令地址,因此不会发生问题。这样理解对么?
Jim_King_2000 2012-04-27
  • 打赏
  • 举报
回复
把void (*)()强制转换成了void (*)(const int32_t, const int32_t, const uint64_t)。
IterZebra 2012-04-27
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

boost做了一个强制转换。
C/C++ code

template<
typename FunctionObj,
typename R BOOST_FUNCTION_COMMA
BOOST_FUNCTION_TEMPLATE_PARMS
>
struct BOOST_FUNCTION_VOID_FUNCTION_OB……
[/Quote]

拜托了。
IterZebra 2012-04-27
  • 打赏
  • 举报
回复
首先 boost::bind(&ABC::transfer_configured_vtn_to_vtn_thread)的结果是一个FunctionObj
应该是 boost::function<void()>的函数对象。

将 boost::function<void()>的函数对象转化成boost::function<void(const int32_t, const int32_t, const uint64_t)>??
IterZebra 2012-04-27
  • 打赏
  • 举报
回复
啥意思?可以详细点么?比如 从 = 这个 运算符说起?
Jim_King_2000 2012-04-27
  • 打赏
  • 举报
回复
boost做了一个强制转换。

template<
typename FunctionObj,
typename R BOOST_FUNCTION_COMMA
BOOST_FUNCTION_TEMPLATE_PARMS
>
struct BOOST_FUNCTION_VOID_FUNCTION_OBJ_INVOKER
{
static BOOST_FUNCTION_VOID_RETURN_TYPE
invoke(function_buffer& function_obj_ptr BOOST_FUNCTION_COMMA
BOOST_FUNCTION_PARMS)

{
FunctionObj* f;
if (function_allows_small_object_optimization<FunctionObj>::value)
f = reinterpret_cast<FunctionObj*>(&function_obj_ptr.data);
else
f = reinterpret_cast<FunctionObj*>(function_obj_ptr.obj_ptr);
BOOST_FUNCTION_RETURN((*f)(BOOST_FUNCTION_ARGS));
}
};
1.41.0版本的中文文档20091225,已完成翻译91个库. 汉化 boost 文档,致力 boost 推广。 如果你对本项目有兴趣,欢迎加入,相关说明请查阅项目论坛: https://groups.google.com/ 到目前为止,各人贡献的译文如下: 贡献者 贡献的译文 alai04 accumulators, any, assign, bimap, call_traits, circular_buffer, compatibility, concept check, config, conversion, date_time, filesystem, foreach, function_types, functional, integer, intrusive, io state savers, iostreams, iterators, minmax, mpl, multi_array, multi_index, numeric/conversion, operators, optional, pointer container, pool, preprocessor, program_options, property map, property_tree, proto, range, rational, scope_exit, serialization, spirit, statechart, swap, system, tokenizer, TR1, tribool, unordered, utility, value_initialized, variant, wave, xpressive fatalerror99 array, bind & mem_fn, dynamic_bitset, function, functional/hash, in_place_factory & typed_in_place_factory, lambda, ref, smart_ptr, static_assert, string_algo, type_traits, typeof farproc compressed_pair, crc, enable_if blackjazz07 conversion/lexical_cast, format hzjboost tuple xuwaters parameter, timer 金庆 python, signals, signals2 zhaohongchao exception, gil luckycat06 interval, math, math/complex number algorithms, math/common_factor, math/octonion, math/quaternion, math/special_functions, math/statistical, uBLAS lixin regex, test, units evy.wang thread xiaq random 水的影子 flyweight
THE BOOST C++ LIBRARIES是一份自己编译的chm格式文档,描述了如何使用boost类库,目录如下: Front page Chapter 1: Introduction 1.1 C++ and Boost 1.2 Development Process 1.3 Installation 1.4 Overview Chapter 2: Smart Pointers 2.1 General 2.2 RAII 2.3 Scoped Pointer 2.4 Scoped Array 2.5 Shared Pointer 2.6 Shared Array 2.7 Weak Pointer 2.8 Intrusive Pointer 2.9 Pointer Container 2.10 Exercises Chapter 3: Function Objects 3.1 General 3.2 Boost.Bind 3.3 Boost.Ref 3.4 Boost.Function 3.5 Boost.Lambda 3.6 Exercises Chapter 4: Event Handling 4.1 General 4.2 Signals 4.3 Connections 4.4 Exercises Chapter 5: String Handling 5.1 General 5.2 Locales 5.3 Boost.StringAlgorithms 5.4 Boost.Regex 5.5 Boost.Tokenizer 5.6 Boost.Format 5.7 Exercises Chapter 6: Multithreading 6.1 General 6.2 Thread Management 6.3 Synchronization 6.4 Thread Local Storage 6.5 Exercises Chapter 7: Asynchronous Input and Output 7.1 General 7.2 I/O Services and I/O Objects 7.3 Scalability and Multithreading 7.4 Network Programming 7.5 Developing Boost.Asio Extensions 7.6 Exercises Chapter 8: Interprocess Communication 8.1 General 8.2 Shared Memory 8.3 Managed Shared Memory 8.4 Synchronization 8.5 Exercises Chapter 9: Filesystem 9.1 General 9.2 Paths 9.3 Files and Directories 9.4 File Streams 9.5 Exercises Chapter 10: Date and Time 10.1 General 10.2 Calendar Dates 10.3 Location-independent Times 10.4 Location-dependent Times 10.5 Formatted Input and Output 10.6 Exercises Chapter 11: Serialization 11.1 General 11.2 Archive 11.3 Pointers and references 11.4 Serialization of class hierarchy objects 11.5 Wrapper functions for optimization 11.6 Exercises Chapter 12: Parser 12.1 General 12.2 Extended Backus-Naur Form 12.3 Grammar 12.4 Actions 12.5 Exercises Chapter 13: Containers 13.1 General 13.2 Boost.Array 13.3 Boost.Unordered 13.4 Boost.MultiIndex 13.5 Boost.Bimap 13.6 Exercises Chapter 14: Data Structures 14.1 General 14.2 Boost.Tuple 14.3 Boost.Any 14.4 Boost.Variant 14.5 Exercises Chapter 15: Error Handling 15.1 General 15.2 Boost.System 15.3 Boost.Exception Chapter 16: Cast Operators

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧