【问题标题】:Using both boost::bind and std::tr1::bind in different parts of the client code在客户端代码的不同部分同时使用 boost::bind 和 std::tr1::bind
【发布时间】:2025-12-31 07:20:15
【问题描述】:

我正在使用两个使用绑定功能的自定义库。 其中一个使用 std::tr1 中的绑定功能,而另一个使用 boost bind

现在问题在于占位符。由于 boost bind 的占位符位于未命名的命名空间中,因此当我尝试编译代码时遇到以下错误

<file_name.cpp:line_no:char_no>: error: reference to '_1' is ambiguous
/usr/include/c++/4.6/tr1/functional:859:23: error: candidates are: std::tr1::_Placeholder<1> std::tr1::placeholders::{anonymous}::_1
<path>/common/include/boost/bind/placeholders.hpp:55:15: error:                 boost::arg<1> {anonymous}::_1

仅从我正在使用的客户端代码中附加使用 boost 绑定功能的行

f_args.f_cb = boost::bind(&MyProcess::flush, MyProcess::Type, _1, _2);

如何解决这个问题?我排除了对库进行更改或使用其他库的可能性。提前致谢。

【问题讨论】:

    标签: c++ boost boost-bind stdbind


    【解决方案1】:

    最终std 中的占位符由特征类标识。

    代替using namespace 占位符,使用traits 类将boost 占位符变成std 占位符。

    如果这不切实际,那么您的 boost 占位符可能也是如此:为两者创建一组新的占位符标记,并将它们用于两者。

    【讨论】:

    • 命名空间包含在库头文件中。
    • @rgk:你是说你的头文件中有using namespace std::tr1::placeholders; 语句吗?
    • @rgk 好的。第3段怎么样?创建新的占位符(相同类型的变量,或使用特征类将其他类型标记为占位符变量)并使用它们。
    • @MikeMB 它在库头文件中不是我的。
    • @Yakk 我不确定你的意思。可以举个例子吗?