这是我尝试编译的代码的简短版本。
#include <iostream>
#include <functional>
#include <memory>
void foo(std::shared_ptr<int> b, unsigned int i)
{
std::cout << *b << " - " << i << std::endl;
}
int main()
{
using namespace std::placeholders;
typedef std::function<void(std::shared_ptr<int> b, unsigned int i)> Foo;
typedef std::function<void(unsigned int code)> Bar;
Foo f1 = foo;
Bar f2 = std::bind(f1, std::make_shared<int>(10), _1);
f2(0);
return 0;
}
g++-4.7 --version
给出 g++-4.7 (GCC) 4.7.1
g++-4.7 -std=c++11 test.cpp && ./a.out
10 - 0
c++ --version
提供 Apple clang 4.0 版(tags/Apple/clang-421.0.60)(基于 LLVM 3.1svn)
c++ -std=c++11 -stdlib=libc++ test.cpp
In file included from test.cpp:3:
In file included from /usr/bin/../lib/c++/v1/iostream:38:
In file included from /usr/bin/../lib/c++/v1/ios:216:
In file included from /usr/bin/../lib/c++/v1/__locale:18:
In file included from /usr/bin/../lib/c++/v1/mutex:177:
/usr/bin/../lib/c++/v1/functional:1642:8: error: no type named 'type' in 'std::__1::__invoke_of<std::__1::function<void (std::__1::shared_ptr<int>, unsigned int)> &,
std::__1::shared_ptr<int> &, std::__1::shared_ptr<int> &&>'
>::type type;
~~~^~~~
/usr/bin/../lib/c++/v1/functional:1721:18: note: in instantiation of template class 'std::__1::__bind_return<std::__1::function<void (std::__1::shared_ptr<int>, unsigned int)>,
std::__1::tuple<std::__1::shared_ptr<int>, std::__1::placeholders::__ph<1>>, std::__1::tuple<std::__1::shared_ptr<int> &&, unsigned int &&>>' requested here
typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type
^
/usr/bin/../lib/c++/v1/functional:1722:9: note: while substituting deduced template arguments into function template 'operator()'
[with _Args = <std::__1::shared_ptr<int>, unsigned int>]
operator()(_Args&& ...__args)
^
/usr/bin/../lib/c++/v1/type_traits:2971:1: note: while substituting deduced template arguments into function template '__invoke' [with _Fp = std::__1::__bind<std::__1::function<void
(std::__1::shared_ptr<int>, unsigned int)> &, std::__1::shared_ptr<int>, std::__1::placeholders::__ph<1> &> &, _Args = <no value>]
__invoke(_Fp&& __f, _Args&& ...__args)
^
/usr/bin/../lib/c++/v1/type_traits:2989:11: note: in instantiation of template class 'std::__1::__invokable_imp<std::__1::__bind<std::__1::function<void
(std::__1::shared_ptr<int>, unsigned int)> &, std::__1::shared_ptr<int>, std::__1::placeholders::__ph<1> &> &, std::__1::shared_ptr<int>, unsigned int>' requested here
__invokable_imp<_Fp, _Args...>::value>
^
/usr/bin/../lib/c++/v1/functional:1115:33: note: in instantiation of template class 'std::__1::__invokable<std::__1::__bind<std::__1::function<void
(std::__1::shared_ptr<int>, unsigned int)> &, std::__1::shared_ptr<int>, std::__1::placeholders::__ph<1> &> &, std::__1::shared_ptr<int>, unsigned int>' requested here
template <class _Fp, bool = __invokable<_Fp&, _ArgTypes...>::value>
^
/usr/bin/../lib/c++/v1/functional:1141:35: note: (skipping 13 contexts in backtrace; use -ftemplate-backtrace-limit=0 to see all)
typename enable_if<__callable<_Fp>::value>::type* = 0);
^
/usr/bin/../lib/c++/v1/memory:2218:20: note: in instantiation of template class 'std::__1::is_nothrow_move_constructible<std::__1::__bind<std::__1::function<void
(std::__1::shared_ptr<int>, unsigned int)> &, std::__1::shared_ptr<int>, std::__1::placeholders::__ph<1> &>>' requested here
_NOEXCEPT_(is_nothrow_move_constructible<_T1>::value &&
^
/usr/bin/../lib/c++/v1/__config:253:34: note: expanded from macro '_NOEXCEPT_'
# define _NOEXCEPT_(x) noexcept(x)
^
/usr/bin/../lib/c++/v1/memory:2386:15: note: in instantiation of template class 'std::__1::__libcpp_compressed_pair_imp<std::__1::__bind<std::__1::function<void
(std::__1::shared_ptr<int>, unsigned int)> &, std::__1::shared_ptr<int>, std::__1::placeholders::__ph<1> &>, std::__1::allocator<std::__1::__bind<std::__1::function<void
(std::__1::shared_ptr<int>, unsigned int)> &, std::__1::shared_ptr<int>, std::__1::placeholders::__ph<1> &>>, 2>' requested here
: private __libcpp_compressed_pair_imp<_T1, _T2>
^
/usr/bin/../lib/c++/v1/functional:988:36: note: in instantiation of template class 'std::__1::__compressed_pair<std::__1::__bind<std::__1::function<void
(std::__1::shared_ptr<int>, unsigned int)> &, std::__1::shared_ptr<int>, std::__1::placeholders::__ph<1> &>, std::__1::allocator<std::__1::__bind<std::__1::function<void
(std::__1::shared_ptr<int>, unsigned int)> &, std::__1::shared_ptr<int>, std::__1::placeholders::__ph<1> &>>>' requested here
__compressed_pair<_Fp, _Alloc> __f_;
^
/usr/bin/../lib/c++/v1/functional:1273:13: note: in instantiation of template class 'std::__1::__function::__func<std::__1::__bind<std::__1::function<void
(std::__1::shared_ptr<int>, unsigned int)> &, std::__1::shared_ptr<int>, std::__1::placeholders::__ph<1> &>, std::__1::allocator<std::__1::__bind<std::__1::function<void
(std::__1::shared_ptr<int>, unsigned int)> &, std::__1::shared_ptr<int>, std::__1::placeholders::__ph<1> &>>, void (unsigned int)>' requested here
if (sizeof(_FF) <= sizeof(__buf_) && is_nothrow_copy_constructible<_Fp>::value)
^
test.cpp:21:12: note: in instantiation of function template specialization 'std::__1::function<void (unsigned int)>::function<std::__1::__bind<std::__1::function<void
(std::__1::shared_ptr<int>, unsigned int)> &, std::__1::shared_ptr<int>, std::__1::placeholders::__ph<1> &> >' requested here
Bar f2 = std::bind(f1, std::make_shared<int>(10), _1);
^
1 error generated.
我的代码有问题吗?
谢谢。