[英]Get point-to type of boost shared_ptr
在我的項目中,我在一個頭文件中使用boost :: shared_ptr寫道:
typedef boost::shared_ptr<boost::lockfree::spsc_queue<PacketsInput, boost::lockfree::capacity<4096> > > queue_ptr;
在另一個源文件中,我使用它:
std::vector<queue_ptr> v;
for (...)
v.push_back(boost::make_shared(/* #1 */));
在#1中,我想編寫queue_ptr的要鍵入的點,而不是
boost::lockfree::spsc_queue<PacketsInput, boost::lockfree::capacity<4096> >
多久了!
但是boost :: shared_ptr中沒有typedef,我發現的唯一一個是typedef: typedef typename boost::detail::sp_element< T >::type element_type;
但是我不知道如何使用它。
有什么幫助嗎? 坦克很多!
文檔說有一個成員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);
}
給定您已經聲明的typedef,可以像這樣使用它:
typedef queue_ptr::element_type elem_type;
v.push_back( boost::make_shared<elem_type>( /* args for ctor */ ) );
您可以使用兩個typedef
typedef boost::lockfree::spsc_queue<PacketsInput, boost::lockfree::capacity<4096> > my_queue;
typedef boost::shared_ptr<my_queue> my_queue_ptr;
然后可以在make_shared
使用my_queue
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.