获取boost的shared_ptr的指向类型

大宝剑*_*大宝剑 1 c++ boost smart-pointers shared-ptr

在我的项目中,我使用 boost::shared_ptr,在一个头文件中,我写道:

typedef boost::shared_ptr<boost::lockfree::spsc_queue<PacketsInput, boost::lockfree::capacity<4096> > > queue_ptr;
Run Code Online (Sandbox Code Playgroud)

在另一个源文件中,我使用它:

std::vector<queue_ptr> v;
for (...)
    v.push_back(boost::make_shared(/* #1 */));
Run Code Online (Sandbox Code Playgroud)

在#1中,我想将queue_ptr的点写入类型,而不是

boost::lockfree::spsc_queue<PacketsInput, boost::lockfree::capacity<4096> >
Run Code Online (Sandbox Code Playgroud)

有多长啊!

但是boost::shared_ptr中没有typedef,我找到的唯一一个typedef:typedef typename boost::detail::sp_element< T >::type element_type;但是我不知道如何使用它。

有什么帮助吗?坦克很多!

jro*_*rok 5

文档说有一个名为 typedef 的成员element_type

此示例程序运行良好(断言通过):

#include <boost/shared_ptr.hpp>
#include <boost/type_traits.hpp>
#include <cassert>

int main()
{
    bool b =  boost::is_same<boost::shared_ptr<int>::element_type, int>::value;
    assert(b);
}
Run Code Online (Sandbox Code Playgroud)

给定您已经声明的 typedef,您可以像这样使用它:

typedef queue_ptr::element_type elem_type;
v.push_back( boost::make_shared<elem_type>( /* args for ctor */ ) );
Run Code Online (Sandbox Code Playgroud)