使用__compressed_pair时出错

Error in use of __compressed_pair

本文关键字:出错 compressed 使用 pair      更新时间:2023-10-16

我开始在libc++ master(见future)之上实现N3558,但我现在在functional中遇到了一个错误,我不理解。

无论我如何编码,它总是失败的__compressed_pair错误。我不知道我是否错过了什么,或者它是libc++中的一个bug。

以下代码:

#include <future>
struct test {
    test() {
        int someint = 0;
        std::promise<void> prom;
        auto fut = prom.get_future();
        fut.then( [this, someint]( std::future<void> future ) {
        } );
    }
};

触发clang 3.3:

In file included from /std/include/map:375:
/std/include/functional:993:11: error: no matching constructor for initialization of '__compressed_pair<<lambda at          /std/include/future:1096:14>, std::__1::allocator<<lambda at    /std/include/future:1096:14> > >'
    : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
      ^    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/std/include/functional:1278:26: note: in instantiation of member function 'std::__1::__function::__func<<lambda at /std/include/future:1096:14>, std::__1::allocator<<lambda at /std/include/future:1096:14> >, void ()>::__func' requested here
        ::new (__f_) _FF(_VSTD::move(__f));
                     ^
/std/include/__functional_base:341:37: note: in instantiation of function template specialization 'std::__1::function<void ()>::function<<lambda at /std/include/future:1096:14> >' requested here
return _VSTD::forward<_Fp>(__f)(_VSTD::forward<_Args>(__args)...);
                                ^
/std/include/__config:301:15: note: expanded from macro '_VSTD'
#define _VSTD std::_LIBCPP_NAMESPACE
          ^
/std/include/functional:1691:12: note: in instantiation of function template specialization 'std::__1::__invoke<const <lambda at /std/include/future:1144:17> &, const <lambda at /std/include/future:1096:14> &>' requested here
return __invoke(__f, __mu(get<_Indx>(__bound_args), __args)...);
       ^
/std/include/functional:1761:20: note: in instantiation of function template specialization 'std::__1::__apply_functor<const <lambda at /std/include/future:1144:17>, const std::__1::tuple<<lambda at /std/include/future:1096:14> >, 0, std::__1::tuple<> >' requested here
        return __apply_functor(__f_, __bound_args_, __indices(),
               ^
/std/include/future:1114:3: note: in instantiation of function template specialization 'std::__1::__bind<<lambda at /std/include/future:1144:17>, <lambda at /std/include/future:1096:14> >::operator()<>' requested here
            invoke_bind();
            ^
/std/include/future:1148:22: note: in instantiation of function template specialization 'std::__1::__then<void>::bind<std::__1::future<void>, <lambda at game/Game_local.cpp:1020:9>, <lambda at /std/include/future:1144:17> >' requested here
    return __then<_Rp>::bind( fut, forward<F>(execute_func), move(invoker) );
                        ^
/std/include/future:1527:34: note: in instantiation of function template specialization 'std::__1::__then<void>::bind_async<std::__1::future<void>, <lambda at game/Game_local.cpp:1020:9> >' requested here
{return __then<return_type>::bind_async( *this, std::forward<Function>(func) );}
                             ^
game/local.cpp:1020:3: note: in instantiation of function template specialization 'std::__1::future<void>::then<<lambda at game/Game_local.cpp:1020:9> >' requested here
    .then( [this, someint]( std::future<void> future ) {
     ^
/std/include/memory:2371:31: note: candidate constructor not viable: requires 0 arguments, but 3 were provided
_LIBCPP_INLINE_VISIBILITY __compressed_pair() {}
                          ^
/std/include/memory:2372:40: note: candidate constructor not viable: requires single argument '__t1', but 3 arguments were provided
_LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T1_param __t1)
                                   ^
/std/include/memory:2374:40: note: candidate constructor not viable: requires single argument '__t2', but 3 arguments were provided
_LIBCPP_INLINE_VISIBILITY explicit __compressed_pair(_T2_param __t2)
                                   ^
/std/include/memory:2376:31: note: candidate constructor not viable: requires 2 arguments, but 3 were provided
_LIBCPP_INLINE_VISIBILITY __compressed_pair(_T1_param __t1, _T2_param __t2)
                          ^
/std/include/memory:2357:7: note: candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 3 were provided
class __compressed_pair
  ^
/std/include/memory:2357:7: note: candidate constructor (the implicit move constructor) not viable: requires 1 argument, but 3 were provided

使用gcc 4.8编译会给出一个难看得多的错误消息,但基本上是一样的。

有人能给点光吗?

基本上,有趣的部分发生在您尝试从bind_async (future:1144)调用调用者的绑定时。这个lambda以std::function作为参数,但是您的绑定在那里有一个lambda。因此,它从lambda构造参数,从而在内部创建类型擦除子类__function::__func__func同时存储lambda和分配器,为了在使用无状态分配器时不浪费空间,它使用__compressed_pair。因此,它尝试用piecewise_construct构造函数构造compressed_pair,但没有找到。

为什么它找不到一个?有问题的构造函数(内存:2416)位于#if块下,特别是

#ifndef _LIBCPP_HAS_NO_VARIADICS

我假设这个符号在你的构建中被定义了。您是否将-std=c++11传递给编译器?如果是的话(还有其他的东西怎么编译?),也许你需要再次检查libc++的配置。在任何情况下,您都应该尝试在上面的预处理器块中添加一个#else,并在其中添加一个#error,以确保这确实是您遇到的问题。