簡體   English   中英

與shared_ptr一起使用g ++ std :: bind錯誤

[英]g++ std::bind error with shared_ptr

我無法理解為什么以下代碼無法編譯。

#include <memory>
#include <functional>

class Foo 
{
public:
  void Bar(int i) {}
};

void X(std::function<void(std::shared_ptr<Foo>)> f)
{

}

int main()
{
  std::shared_ptr<Foo> f(new Foo);
  auto f1(std::bind(&Foo::Bar, std::placeholders::_1, 1));
  X(f1);
  return 0;
}

g ++(4.6.3)輸出......

n file included from /usr/include/c++/4.6/memory:80:0,
                 from test.cpp:1:
/usr/include/c++/4.6/functional: In static member function ‘static void std::_Function_handler<void(_ArgTypes ...), _Functor>::_M_invoke(const std::_Any_data&, _ArgTypes ...) [with _Functor = std::_Bind<std::_Mem_fn<void (Foo::*)(int)>(std::_Placeholder<1>, int)>, _ArgTypes = {std::shared_ptr<Foo>}]’:
/usr/include/c++/4.6/functional:2148:6:   instantiated from ‘std::function<_Res(_ArgTypes ...)>::function(_Functor, typename std::enable_if<(! std::is_integral<_Functor>::value), std::function<_Res(_ArgTypes ...)>::_Useless>::type) [with _Functor = std::_Bind<std::_Mem_fn<void (Foo::*)(int)>(std::_Placeholder<1>, int)>, _Res = void, _ArgTypes = {std::shared_ptr<Foo>}, typename std::enable_if<(! std::is_integral<_Functor>::value), std::function<_Res(_ArgTypes ...)>::_Useless>::type = std::function<void(std::shared_ptr<Foo>)>::_Useless]’
test.cpp:19:7:   instantiated from here
/usr/include/c++/4.6/functional:1778:2: error: no match for call to ‘(std::_Bind<std::_Mem_fn<void (Foo::*)(int)>(std::_Placeholder<1>, int)>) (std::shared_ptr<Foo>)’
/usr/include/c++/4.6/functional:1130:11: note: candidates are:
/usr/include/c++/4.6/functional:1201:2: note: template<class ... _Args, class _Result> _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) [with _Args = {_Args ...}, _Result = _Result, _Functor = std::_Mem_fn<void (Foo::*)(int)>, _Bound_args = {std::_Placeholder<1>, int}]
/usr/include/c++/4.6/functional:1215:2: note: template<class ... _Args, class _Result> _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) const [with _Args = {_Args ...}, _Result = _Result, _Functor = std::_Mem_fn<void (Foo::*)(int)>, _Bound_args = {std::_Placeholder<1>, int}]
/usr/include/c++/4.6/functional:1229:2: note: template<class ... _Args, class _Result> _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) volatile [with _Args = {_Args ...}, _Result = _Result, _Functor = std::_Mem_fn<void (Foo::*)(int)>, _Bound_args = {std::_Placeholder<1>, int}]
/usr/include/c++/4.6/functional:1243:2: note: template<class ... _Args, class _Result> _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) const volatile [with _Args = {_Args ...}, _Result = _Result, _Functor = std::_Mem_fn<void (Foo::*)(int)>, _Bound_args = {std::_Placeholder<1>, int}]

這是一個GCC錯誤,我在4.7中修復過,但我不記得究竟是哪個錯誤。 我會試着搞清楚......

編輯:啊哈,這是PR 55463所以不是固定在4.7,它只固定在GCC主干上(將是4.8)

錯誤是mem_fn返回的調用包裝器不接受rvalues,並且你的std::function<void(std::shared_ptr<Foo>)類型將rvalue shared_ptr<Foo>傳遞給bind返回的調用包裝器。

作為解決方法,您可以將功能簽名更改為:

void X(std::function<void(const std::shared_ptr<Foo>&)> f)

我不認為我可以將修復程序向后移植到4.7分支,因為由於該錯誤,我對mem_fn進行了一些非常大的更改,這些更改並不適合穩定版本分支(我還發現了一個新的缺陷 )標准)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM