iOS   发布时间:2022-05-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了xcode – c functional / std :: bind不能用Apple Clang编译大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

这是我尝试编译的代码的简短版本. #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 n
这是我尝试编译的代码的简短版本.

#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版(标签/ 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>,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>' 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>' 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>,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>,std::__1::placeholders::__ph<1> &>,std::__1::allocator<std::__1::__bind<std::__1::function<void
      (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>,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>,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>,std::__1::placeholders::__ph<1> &> >' requested here
  Bar f2 = std::bind(f1,_1);
           ^
1 error generated.

我的代码有问题吗?

谢谢.

解决方法

对此bug感到抱歉.

这看起来像是一个副本:

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,const tuple<_BoundArgs...>,+                    typename __mu_return<const _BoundArgs,class _TupleUj,+          bool = _is_valid_bind_return<_Fp,_BoundArgs,_TupleUj>::value>
 struct __bind_return;

 template <class _Fp,class _TupleUj>
-struct __bind_return<_Fp,_TupleUj>
+struct __bind_return<_Fp,_TupleUj,true>
 {
     typedef typename __invoke_of
     <
@@ -1647,7 +1669,7 @@
 };

 template <class _Fp,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,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,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,+            result_type
+        >::type
         operator()(_Args&& ...__args) const
         {
             return base::operator()(_VSTD::forWARD<_Args>(__args)...);
@H_404_50@

大佬总结

以上是大佬教程为你收集整理的xcode – c functional / std :: bind不能用Apple Clang编译全部内容,希望文章能够帮你解决xcode – c functional / std :: bind不能用Apple Clang编译所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。