我正在尝试在我的代码中使用boost::circular_buffer,如下所示:

class EventRegistry
{
public:
    EventRegistry()
    {
        pendingEvents_.set_capacity(MAX_PENDING_EVENTS);
    }

private:
    boost::circular_buffer<boost::movelib::unique_ptr<EventData>> pendingEvents_;
}


但是,我遇到了构建失败(摘录):

...
boost/build/binaries/include/boost/circular_buffer/details.hpp:470:12: note: in instantiation of function template specialization 'boost::cb_details::uninitialized_move_if_noexcept_impl<boost::cb_details::iterator<boost::circular_buffer<boost::movelib::unique_ptr<appd::agent::EventData, boost::movelib::default_delete<appd::agent::EventData> >, std::allocator<boost::movelib::unique_ptr<appd::agent::EventData, boost::movelib::default_delete<appd::agent::EventData> > > >, boost::cb_details::nonconst_traits<boost::container::allocator_traits<std::allocator<boost::movelib::unique_ptr<appd::agent::EventData, boost::movelib::default_delete<appd::agent::EventData> > > > > >, boost::movelib::unique_ptr<appd::agent::EventData, boost::movelib::default_delete<appd::agent::EventData> > *, std::allocator<boost::movelib::unique_ptr<appd::agent::EventData, boost::movelib::default_delete<appd::agent::EventData> > > >' requested here
    return uninitialized_move_if_noexcept_impl(first, last, dest, a, tag_t());
           ^
boost/build/binaries/include/boost/circular_buffer/base.hpp:955:37: note: in instantiation of function template specialization 'boost::cb_details::uninitialized_move_if_noexcept<boost::cb_details::iterator<boost::circular_buffer<boost::movelib::unique_ptr<appd::agent::EventData, boost::movelib::default_delete<appd::agent::EventData> >, std::allocator<boost::movelib::unique_ptr<appd::agent::EventData, boost::movelib::default_delete<appd::agent::EventData> > > >, boost::cb_details::nonconst_traits<boost::container::allocator_traits<std::allocator<boost::movelib::unique_ptr<appd::agent::EventData, boost::movelib::default_delete<appd::agent::EventData> > > > > >, boost::movelib::unique_ptr<appd::agent::EventData, boost::movelib::default_delete<appd::agent::EventData> > *, std::allocator<boost::movelib::unique_ptr<appd::agent::EventData, boost::movelib::default_delete<appd::agent::EventData> > > >' requested here
            reset(buff, cb_details::uninitialized_move_if_noexcept(e - (std::min)(new_capacity, size()),
                                    ^
events/event_registry.cc:22:18: note: in instantiation of member function 'boost::circular_buffer<boost::movelib::unique_ptr<appd::agent::EventData, boost::movelib::default_delete<appd::agent::EventData> >, std::allocator<boost::movelib::unique_ptr<appd::agent::EventData, boost::movelib::default_delete<appd::agent::EventData> > > >::rset_capacity' requested here
  pendingEvents_.rset_capacity(MAX_PENDING_EVENTS);
...


但是,如果在构造函数中使用初始化程序:

EventRegistry()
  : pendingEvents_(MAX_PENDING_EVENTS)


然后编译良好。有什么线索吗?

更新:这是完整的输出(很抱歉,长度)

boost/build/binaries/include/boost/container/allocator_traits.hpp:408:51: error: no matching constructor for initialization of 'boost::movelib::unique_ptr<appd::agent::EventData, boost::movelib::default_delete<appd::agent::EventData> >'
      {  ::new((void*)p, boost_container_new_t()) T(::boost::forward<Args>(args)...); }
                                                  ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
boost/build/binaries/include/boost/container/allocator_traits.hpp:353:28: note: in instantiation of function template specialization 'boost::container::allocator_traits<std::allocator<boost::movelib::unique_ptr<appd::agent::EventData, boost::movelib::default_delete<appd::agent::EventData> > > >::priv_construct<boost::movelib::unique_ptr<appd::agent::EventData, boost::movelib::default_delete<appd::agent::EventData> >, boost::movelib::unique_ptr<appd::agent::EventData, boost::movelib::default_delete<appd::agent::EventData> > >' requested here
         allocator_traits::priv_construct(flag, a, p, ::boost::forward<Args>(args)...);
                           ^
boost/build/binaries/include/boost/circular_buffer/details.hpp:439:56: note: in instantiation of function template specialization 'boost::container::allocator_traits<std::allocator<boost::movelib::unique_ptr<appd::agent::EventData, boost::movelib::default_delete<appd::agent::EventData> > > >::construct<boost::movelib::unique_ptr<appd::agent::EventData, boost::movelib::default_delete<appd::agent::EventData> >, boost::movelib::unique_ptr<appd::agent::EventData, boost::movelib::default_delete<appd::agent::EventData> > >' requested here
            boost::container::allocator_traits<Alloc>::construct(a, boost::addressof(*dest), *first);
                                                       ^
boost/build/binaries/include/boost/circular_buffer/details.hpp:460:12: note: in instantiation of function template specialization 'boost::cb_details::uninitialized_copy<boost::cb_details::iterator<boost::circular_buffer<boost::movelib::unique_ptr<appd::agent::EventData, boost::movelib::default_delete<appd::agent::EventData> >, std::allocator<boost::movelib::unique_ptr<appd::agent::EventData, boost::movelib::default_delete<appd::agent::EventData> > > >, boost::cb_details::nonconst_traits<boost::container::allocator_traits<std::allocator<boost::movelib::unique_ptr<appd::agent::EventData, boost::movelib::default_delete<appd::agent::EventData> > > > > >, boost::movelib::unique_ptr<appd::agent::EventData, boost::movelib::default_delete<appd::agent::EventData> > *, std::allocator<boost::movelib::unique_ptr<appd::agent::EventData, boost::movelib::default_delete<appd::agent::EventData> > > >' requested here
    return uninitialized_copy(first, last, dest, a);
           ^
boost/build/binaries/include/boost/circular_buffer/details.hpp:470:12: note: in instantiation of function template specialization 'boost::cb_details::uninitialized_move_if_noexcept_impl<boost::cb_details::iterator<boost::circular_buffer<boost::movelib::unique_ptr<appd::agent::EventData, boost::movelib::default_delete<appd::agent::EventData> >, std::allocator<boost::movelib::unique_ptr<appd::agent::EventData, boost::movelib::default_delete<appd::agent::EventData> > > >, boost::cb_details::nonconst_traits<boost::container::allocator_traits<std::allocator<boost::movelib::unique_ptr<appd::agent::EventData, boost::movelib::default_delete<appd::agent::EventData> > > > > >, boost::movelib::unique_ptr<appd::agent::EventData, boost::movelib::default_delete<appd::agent::EventData> > *, std::allocator<boost::movelib::unique_ptr<appd::agent::EventData, boost::movelib::default_delete<appd::agent::EventData> > > >' requested here
    return uninitialized_move_if_noexcept_impl(first, last, dest, a, tag_t());
           ^
boost/build/binaries/include/boost/circular_buffer/base.hpp:880:29: note: in instantiation of function template specialization 'boost::cb_details::uninitialized_move_if_noexcept<boost::cb_details::iterator<boost::circular_buffer<boost::movelib::unique_ptr<appd::agent::EventData, boost::movelib::default_delete<appd::agent::EventData> >, std::allocator<boost::movelib::unique_ptr<appd::agent::EventData, boost::movelib::default_delete<appd::agent::EventData> > > >, boost::cb_details::nonconst_traits<boost::container::allocator_traits<std::allocator<boost::movelib::unique_ptr<appd::agent::EventData, boost::movelib::default_delete<appd::agent::EventData> > > > > >, boost::movelib::unique_ptr<appd::agent::EventData, boost::movelib::default_delete<appd::agent::EventData> > *, std::allocator<boost::movelib::unique_ptr<appd::agent::EventData, boost::movelib::default_delete<appd::agent::EventData> > > >' requested here
                cb_details::uninitialized_move_if_noexcept(b, b + (std::min)(new_capacity, size()), buff, m_alloc),
                            ^
.../events/event_registry.cc:22:18: note: in instantiation of member function 'boost::circular_buffer<boost::movelib::unique_ptr<appd::agent::EventData, boost::movelib::default_delete<appd::agent::EventData> >, std::allocator<boost::movelib::unique_ptr<appd::agent::EventData, boost::movelib::default_delete<appd::agent::EventData> > > >::set_capacity' requested here
  pendingEvents_.set_capacity(MAX_EVENTS_PER_TIMEWINDOW);
                 ^
boost/build/binaries/include/boost/move/unique_ptr.hpp:360:35: note: candidate constructor not viable: 1st argument ('const boost::movelib::unique_ptr<appd::agent::EventData, boost::movelib::default_delete<appd::agent::EventData> >') would lose const qualifier
   BOOST_MOVABLE_BUT_NOT_COPYABLE(unique_ptr)
                                  ^
boost/build/binaries/include/boost/move/core.hpp:242:46: note: expanded from macro 'BOOST_MOVABLE_BUT_NOT_COPYABLE'
      BOOST_MOVE_IMPL_NO_COPY_CTOR_OR_ASSIGN(TYPE)\
                                             ^
boost/build/binaries/include/boost/move/core.hpp:35:7: note: expanded from macro 'BOOST_MOVE_IMPL_NO_COPY_CTOR_OR_ASSIGN'
      TYPE(TYPE &);\
      ^
boost/build/binaries/include/boost/move/unique_ptr.hpp:401:20: note: candidate constructor not viable: no known conversion from 'const boost::movelib::unique_ptr<appd::agent::EventData, boost::movelib::default_delete<appd::agent::EventData> >' to 'bmupd::nullptr_type' (aka 'int (boost::move_upd::bool_conversion::*)()') for 1st argument
   BOOST_CONSTEXPR unique_ptr(BOOST_MOVE_DOC0PTR(bmupd::nullptr_type)) BOOST_NOEXCEPT
                   ^
boost/build/binaries/include/boost/move/unique_ptr.hpp:527:4: note: candidate constructor not viable: no known conversion from 'const boost::movelib::unique_ptr<appd::agent::EventData, boost::movelib::default_delete<appd::agent::EventData> >' to '::boost::rv<unique_ptr<EventData, default_delete<EventData> > > &' for 1st argument
   unique_ptr(BOOST_RV_REF(unique_ptr) u) BOOST_NOEXCEPT
   ^
boost/build/binaries/include/boost/move/unique_ptr.hpp:424:13: note: candidate template ignored: substitution failure [with Pointer = boost::movelib::unique_ptr<appd::agent::EventData, boost::movelib::default_delete<appd::agent::EventData> >]: no type named 'type' in 'boost::move_upd::enable_up_ptr<appd::agent::EventData, boost::movelib::unique_ptr<appd::agent::EventData, boost::movelib::default_delete<appd::agent::EventData> >, appd::agent::EventData *, boost::move_upmu::nat>'
   explicit unique_ptr(Pointer p
            ^
boost/build/binaries/include/boost/move/unique_ptr.hpp:462:4: note: candidate constructor template not viable: requires at least 2 arguments, but 1 was provided
   unique_ptr(Pointer p, BOOST_MOVE_SEEDOC(deleter_arg_type1) d1
   ^
boost/build/binaries/include/boost/move/unique_ptr.hpp:500:4: note: candidate constructor template not viable: requires at least 2 arguments, but 1 was provided
   unique_ptr(Pointer p, BOOST_MOVE_SEEDOC(deleter_arg_type2) d2
   ^
boost/build/binaries/include/boost/move/unique_ptr.hpp:547:4: note: candidate template ignored: instantiation would take its own class type by value
   unique_ptr( BOOST_RV_REF_BEG_IF_CXX11 unique_ptr<U, E> BOOST_RV_REF_END_IF_CXX11 u
   ^
boost/build/binaries/include/boost/move/unique_ptr.hpp:390:20: note: candidate constructor not viable: requires 0 arguments, but 1 was provided
   BOOST_CONSTEXPR unique_ptr() BOOST_NOEXCEPT
                   ^
boost/build/binaries/include/boost/move/unique_ptr.hpp:475:4: note: candidate constructor not viable: requires 2 arguments, but 1 was provided
   unique_ptr(BOOST_MOVE_DOC0PTR(bmupd::nullptr_type), BOOST_MOVE_SEEDOC(deleter_arg_type1) d1) BOOST_NOEXCEPT
   ^
boost/build/binaries/include/boost/move/unique_ptr.hpp:513:4: note: candidate constructor not viable: requires 2 arguments, but 1 was provided
   unique_ptr(BOOST_MOVE_DOC0PTR(bmupd::nullptr_type), BOOST_MOVE_SEEDOC(deleter_arg_type2) d2) BOOST_NOEXCEPT
   ^
1 error generated.

最佳答案

以下编译就可以了...

#include <boost/circular_buffer.hpp>
#include <boost/move/unique_ptr.hpp>

class EventRegistry
{
public:
    EventRegistry()
    {
        pendingEvents_.set_capacity(10);
    }

private:
    boost::circular_buffer<boost::movelib::unique_ptr<int>> pendingEvents_;
};

int main()
{
    return 0;
}


请注意,我在您放置int的位置放置了EventData
您的问题可能与EventData有关。

(我的观点不足,无法发表评论...)

编辑:哦,检查EventData具有什么样的构造函数!你有默认的构造函数吗?

10-06 06:41