【问题标题】:c++ functional / std::bind doesn't compile with Apple Clangc++ 功能/std::bind 不能用 Apple Clang 编译
【发布时间】:2013-05-23 11:41:44
【问题描述】:

这是我尝试编译的代码的简短版本。

#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.

我的代码有问题吗?

谢谢。

【问题讨论】:

    标签: xcode c++11 compilation clang


    【解决方案1】:

    对这个错误感到抱歉。

    这看起来像:

    http://llvm.org/bugs/show_bug.cgi?id=15295

    这是一个修复它的补丁:

    Index: include/functional
    ===================================================================
    --- include/functional  (revision 175515)
    +++ include/functional  (working copy)
    @@ -1624,16 +1624,38 @@
         : public ____mu_return<_Ti,
                                __is_reference_wrapper<_Ti>::value,
                                is_bind_expression<_Ti>::value,
    -                           0 < is_placeholder<_Ti>::value,
    +                           0 < is_placeholder<_Ti>::value &&
    +                           is_placeholder<_Ti>::value <= tuple_size<_TupleUj>::value,
                                _TupleUj>
     {
     };
    
     template <class _Fp, class _BoundArgs, class _TupleUj>
    +struct _is_valid_bind_return
    +{
    +    static const bool value = false;
    +};
    +
    +template <class _Fp, class ..._BoundArgs, class _TupleUj>
    +struct _is_valid_bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj>
    +{
    +    static const bool value = __invokable<_Fp,
    +                    typename __mu_return<_BoundArgs, _TupleUj>::type...>::value;
    +};
    +
    +template <class _Fp, class ..._BoundArgs, class _TupleUj>
    +struct _is_valid_bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj>
    +{
    +    static const bool value = __invokable<_Fp,
    +                    typename __mu_return<const _BoundArgs, _TupleUj>::type...>::value;
    +};
    +
    +template <class _Fp, class _BoundArgs, class _TupleUj,
    +          bool = _is_valid_bind_return<_Fp, _BoundArgs, _TupleUj>::value>
     struct __bind_return;
    
     template <class _Fp, class ..._BoundArgs, class _TupleUj>
    -struct __bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj>
    +struct __bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj, true>
     {
         typedef typename __invoke_of
         <
    @@ -1647,7 +1669,7 @@
     };
    
     template <class _Fp, class ..._BoundArgs, class _TupleUj>
    -struct __bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj>
    +struct __bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj, true>
     {
         typedef typename __invoke_of
         <
    @@ -1673,8 +1695,10 @@
     class __bind
         : public __weak_result_type<typename decay<_Fp>::type>
     {
    +protected:
         typedef typename decay<_Fp>::type _Fd;
         typedef tuple<typename decay<_BoundArgs>::type...> _Td;
    +private:
         _Fd __f_;
         _Td __bound_args_;
    
    @@ -1731,7 +1755,7 @@
    
         template <class ..._Args>
             _LIBCPP_INLINE_VISIBILITY
    -        typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type
    +        typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type
             operator()(_Args&& ...__args) const
             {
                 return __apply_functor(__f_, __bound_args_, __indices(),
    @@ -1747,6 +1771,8 @@
         : public __bind<_Fp, _BoundArgs...>
     {
         typedef __bind<_Fp, _BoundArgs...> base;
    +    typedef typename base::_Fd _Fd;
    +    typedef typename base::_Td _Td;
     public:
         typedef _Rp result_type;
    
    @@ -1784,7 +1810,12 @@
    
         template <class ..._Args>
             _LIBCPP_INLINE_VISIBILITY
    -        result_type
    +        typename enable_if
    +        <
    +            is_convertible<typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type,
    +                           result_type>::value,
    +            result_type
    +        >::type
             operator()(_Args&& ...__args)
             {
                 return base::operator()(_VSTD::forward<_Args>(__args)...);
    @@ -1792,7 +1823,12 @@
    
         template <class ..._Args>
             _LIBCPP_INLINE_VISIBILITY
    -        result_type
    +        typename enable_if
    +        <
    +            is_convertible<typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type,
    +                           result_type>::value,
    +            result_type
    +        >::type
             operator()(_Args&& ...__args) const
             {
                 return base::operator()(_VSTD::forward<_Args>(__args)...);
    

    【讨论】:

      【解决方案2】:

      它适用于带有 clang 3.2 的 Ubuntu(listdc++ 而不是 libc++),因此在您的代码中没有问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-10-07
        • 2022-12-20
        • 1970-01-01
        • 2018-10-29
        • 1970-01-01
        • 1970-01-01
        • 2015-03-18
        相关资源
        最近更新 更多