【发布时间】:2015-12-17 12:33:58
【问题描述】:
我需要使用一些使用std::tr1::shared_ptr 的旧代码。我需要包含的标头同时具有#include <tr1/memory> 和#include <tr1/functional>。在我的代码中,我想使用std::function 和std::bind,因为我们的编译器支持这些。
这适用于 gcc 4.6.3 和 4.7.3。在 4.9.2、5.1.x 和 5.2.0 中,这会导致编译器错误。这似乎是由于<tr1/functional>-include 而发生的。
我写了一个小例子来重现这个问题:
#include <tr1/memory>
#include <functional>
#include <tr1/functional>
struct Foo {
void test(std::tr1::shared_ptr<int> i) {}
};
int main() {
Foo f;
std::function<void(std::tr1::shared_ptr<int>)> func = std::bind(&Foo::test, f, std::placeholders::_1);
return 0;
}
gcc-4.6.3,没有#include <tr1/functional>,编译OK。在我的本地机器上测试。
gcc-4.6.3,带有#include <tr1/functional>,编译OK。在我的本地机器上测试。
gcc-5.1,没有#include <tr1/functional>,编译OK:http://ideone.com/XrkAXT
gcc-5.1,带有#include <tr1/functional>,编译器错误:http://ideone.com/sRWQLn
我根本没有使用任何std::tr1::function 或std::tr1::bind,我在遗留代码中唯一使用的是std::tr1::shared_ptr 对象。
为什么#include <tr1/functional> 会影响非tr1 std::function / std::bind?或者这里发生了什么?
编辑:根据GNU C++ Library Manual, Chapter 3:“第二条规则的一个特例是 TR1 和 C++11 工具的混合。有可能(尽管不是特别谨慎)包括 TR1 版本和同一个翻译单元中的 C++11 版本的 header"
【问题讨论】:
-
Clang 更有用,它报告了ambiguity。我猜是 ADL。
-
不是因为你同时包含了函数式和 tr1/函数式吗?
-
是 boost 的 tr1/functional 吗?
-
@sop No.(评论太短)
-
使用tr1的旧代码是什么?我认为它应该移植到 c++11,没有更多的 tr1。 tr1 在 C++11 中不再存在。我只使用内存(不是 tr1/内存),功能(不是 tr1/功能)。我认为您应该将所有 std::tr1:: 替换为 std::。还是您使用 2 个编译器?