【问题标题】:Clang bug with std::function, std::bind and std::ref?带有 std::function、std::bind 和 std::ref 的 Clang 错误?
【发布时间】:2014-01-22 13:00:52
【问题描述】:

以下代码在clang(llvm 5.0版)下似乎无法编译:

#include <functional>

int main()
{
    int i;
    std::function<void(int&)> f;
    std::function<void()> f2 = std::bind(f, std::ref(i));
}

如果f 声明如下(即,不是std::function),那么它编译得很好:

void f(int& n1);

是我做错了什么还是真的是编译器错误?

这是我得到的编译器错误:

In file included from test.cpp:1:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/functional:465:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/memory:599:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/tuple:234:73: error: no matching constructor for initialization of 'std::__1::reference_wrapper<int>'
             _NOEXCEPT_(is_nothrow_default_constructible<_Hp>::value) : value()
                                                                        ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/tuple:447:23: note: in instantiation of member function 'std::__1::__tuple_leaf<0, std::__1::reference_wrapper<int>, false>::__tuple_leaf' requested here
    _LIBCPP_CONSTEXPR __tuple_impl()
                      ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/tuple:550:23: note: in instantiation of member function 'std::__1::__tuple_impl<std::__1::__tuple_indices<0>, std::__1::reference_wrapper<int> >::__tuple_impl' requested
      here
    _LIBCPP_CONSTEXPR tuple()
                      ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/functional:1744:11: note: in instantiation of member function 'std::__1::tuple<std::__1::reference_wrapper<int> >::tuple' requested here
          __bound_args_(_VSTD::forward<_BA>(__bound_args)...) {}
          ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/memory:2243:15: note: in instantiation of function template specialization 'std::__1::__bind<std::__1::function<void (int &)> &, std::__1::reference_wrapper<int>
      >::__bind<std::__1::__bind<std::__1::function<void (int &)> &, std::__1::reference_wrapper<int> >, , void>' requested here
              __first_(_VSTD::forward<_Args1>(get<_I1>(__first_args))...)
              ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/memory:2421:15: note: in instantiation of function template specialization 'std::__1::__libcpp_compressed_pair_imp<std::__1::__bind<std::__1::function<void (int &)> &,
      std::__1::reference_wrapper<int> >, std::__1::allocator<std::__1::__bind<std::__1::function<void (int &)> &, std::__1::reference_wrapper<int> > >, 2>::__libcpp_compressed_pair_imp<std::__1::__bind<std::__1::function<void (int &)> &,
      std::__1::reference_wrapper<int> > &&, , 0, >' requested here
            : base(__pc, _VSTD::move(__first_args), _VSTD::move(__second_args),
              ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/functional:992:11: note: in instantiation of function template specialization 'std::__1::__compressed_pair<std::__1::__bind<std::__1::function<void (int &)> &,
      std::__1::reference_wrapper<int> >, std::__1::allocator<std::__1::__bind<std::__1::function<void (int &)> &, std::__1::reference_wrapper<int> > > >::__compressed_pair<std::__1::__bind<std::__1::function<void (int &)> &, std::__1::reference_wrapper<int> > &&, >'
      requested here
        : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
          ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/functional:1277:26: note: in instantiation of member function 'std::__1::__function::__func<std::__1::__bind<std::__1::function<void (int &)> &,
      std::__1::reference_wrapper<int> >, std::__1::allocator<std::__1::__bind<std::__1::function<void (int &)> &, std::__1::reference_wrapper<int> > >, void ()>::__func' requested here
            ::new (__f_) _FF(_VSTD::move(__f));
                         ^
test.cpp:7:29: note: in instantiation of function template specialization 'std::__1::function<void ()>::function<std::__1::__bind<std::__1::function<void (int &)> &, std::__1::reference_wrapper<int> > >' requested here
        std::function<void()> f2 = std::bind(f, std::ref(i));
                                   ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/__functional_base:365:31: note: candidate constructor not viable: requires single argument '__f', but no arguments were provided
    _LIBCPP_INLINE_VISIBILITY reference_wrapper(type& __f) _NOEXCEPT : __f_(&__f) {}
                              ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/__functional_base:367:14: note: candidate constructor not viable: requires 1 argument, but 0 were provided
    private: reference_wrapper(type&&); public: // = delete; // do not bind to temps
             ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/__functional_base:354:24: note: candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 0 were provided
class _LIBCPP_TYPE_VIS reference_wrapper
                       ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/__functional_base:354:24: note: candidate constructor (the implicit move constructor) not viable: requires 1 argument, but 0 were provided
1 error generated.

【问题讨论】:

  • 除了你提到的问题之外,似乎还有很多其他问题;也许你应该先解决它们。
  • 对我来说看起来像一个错误。 GCC 4.8.1 和 Clang (3.5trunk) 都编译得很好。
  • @scott 还有什么问题?我已经用完整的示例代码和这个简单程序的错误输出更新了帖子,以明确没有其他代码给出问题。
  • @DanielFrey Coliru 使用 libstdc++ 还是 libc++?如果 libstdc++ 和 libc++ 之间存在差异,那也可以解释它,并且仍然会在较新的 clang 版本中失败。
  • @hvd 默认情况下,他们似乎使用 libstdc++,如果你提供 -stdlib=libc++,linker fails。嗯,有人知道怎么解决吗?

标签: c++ c++11 compiler-errors clang


【解决方案1】:

该错误已在 5.1 版本中修复

【讨论】:

  • @CanÜrek 该代码在我使用 XCode 5.0 时无法编译。他们一发布,我就将 XCode(因此也发出了叮当声)更新到了 5.1。之后代码编译完美。我有一个使用类似代码的项目,如果它没有修复,宣布我终于可以在 mac 上编译我的库是很愚蠢的,不是吗?我现在没有要检查的 mac,但至少当我发布它时,错误已修复。
猜你喜欢
  • 1970-01-01
  • 2013-04-16
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多