gpt4 book ai didi

c++ - 将 std::tr1::shared_ptr 与 std::function/std::bind 混合会导致较新的 gcc 出现编译器错误

转载 作者:太空狗 更新时间:2023-10-29 20:23:10 25 4
gpt4 key购买 nike

我需要使用一些使用 std::tr1::shared_ptr 的旧遗留代码.我需要包含的 header 同时包含 #include <tr1/memory>#include <tr1/functional> .在我的代码中,我想使用 std::functionstd::bind ,因为我们的编译器支持这些。

这适用于 gcc 4.6.3 和 4.7.3。在 4.9.2、5.1.x 和 5.2.0 中,这会导致编译器错误。这似乎是由于 <tr1/functional> 而发生的-包括。

我写了一个重现这个问题的小例子:

#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> ,编译成功:http://ideone.com/XrkAXT

gcc-5.1,带有 #include <tr1/functional> ,编译器错误:http://ideone.com/sRWQLn

我没有使用任何 std::tr1::functionstd::tr1::bind完全没有,我从遗留代码中唯一使用的是 std::tr1::shared_ptr对象。

为什么 #include <tr1/functional>影响非tr1 std::function/std::bind ?或者这里发生了什么?

编辑:根据GNU C++ Library Manual, Chapter 3 : "第二条规则的一个特例是 TR1 和 C++11 工具的混合。可以(尽管不是特别谨慎)在相同的翻译单元"

最佳答案

标准发生了变化,需要实现来约束 std::function的构造函数,因此它不能从阳光下的一切转换,而只能从实际的可调用对象转换,因此 libstdc++ 计算返回类型:

template<typename _Functor>
using _Invoke = decltype(__callable_functor(std::declval<_Functor&>())
(std::declval<_ArgTypes>()...) );

并在 SFINAE 中使用它。表达式 SFINAE 意味着如果调用表达式未编译,则转换构造函数将从重载集中删除。

问题在于 __callable_functor .它的工作是包装指向成员的指针,以便它们可以与正常的函数调用语法一起使用。而且它是一个保留名称,所以其他人不应该使用它……好吧,标准库的其他组件除外。我们有std::__callable_functor<functional>std::tr1::__callable_functor<tr1/functional> .因为std::tr1是关联的命名空间(由于在 std::tr1::shared_ptr<int> 的签名中使用了 Foo::test),您最终会产生歧义。

编辑:报告为bug 68995 .

关于c++ - 将 std::tr1::shared_ptr 与 std::function/std::bind 混合会导致较新的 gcc 出现编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34334735/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com