Casting shared_ptr to templatized class

82 views Asked by At

I am trying to cast a shared pointer to a base class to a shared pointer to a templatized derived class when using a library (UHD). In particular, I have obtained a shared pointer to the class filter_info_base (here) and I would like to cast it to a pointer to a digital_filter_base class (here), since this would allow me to get the taps of the filter and the other parameters via the proper getters.

I have searched the web for hours, but all the solutions I have obtained were not working. In particular, the answer seems to be "use std::dynamic_pointer_cast", but if I do

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

(where a_rx_FIR1 is the sptr to the base class) I get from 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);

Has anyone got an idea about how to do this cast?

Thanks in advance and have a nice day!

RH

1

There are 1 answers

3
Arpegius On BEST ANSWER

That is becouse uhd::filter_info_base::sptr is a boost shared pointer:

typedef boost::shared_ptr< filter_info_base > sptr;

And std::dynamic_pointer_cast works only for std::shared_ptr, you cannot mix them and you need to use boost version:

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