将 shared_ptr 转换为模板化 class

Casting shared_ptr to templatized class

我试图在使用库 (UHD) 时将指向基 class 的共享指针转换为指向模板化派生 class 的共享指针。 特别是,我获得了指向 class filter_info_base (here) and I would like to cast it to a pointer to a digital_filter_base class (here) 的共享指针,因为这将允许我通过适当的 getter 获取过滤器的抽头和其他参数。

我在网上搜索了几个小时,但我获得的所有解决方案均无效。特别是,答案似乎是"use std::dynamic_pointer_cast",但如果我这样做

uhd::digital_filter_base<int16_t>::sptr p = std::dynamic_pointer_cast<uhd::digital_filter_base<int16_t>>(a_rx_FIR1);

(其中 a_rx_FIR1 是基数 class 的 sptr)我从 GCC 得到:

error: no matching function for call to ‘dynamic_pointer_cast(uhd::filter_info_base::sptr&)’
 uhd::digital_filter_base<int16_t>::sptr p = std::dynamic_pointer_cast<uhd::digital_filter_base<int16_t>>(a_rx_FIR1);

有没有人知道如何进行此转换?

在此先致谢,祝您有愉快的一天!

相对湿度

那是因为uhd::filter_info_base::sptr是一个boost共享指针:

typedef boost::shared_ptr< filter_info_base > sptr;

std::dynamic_pointer_cast只适用于std::shared_ptr,不能混用,需要使用boost version:

uhd::digital_filter_base<int16_t>::sptr p = boost::dynamic_pointer_cast<uhd::digital_filter_base<int16_t>>(a_rx_FIR1);