如何使用另一个模板化类的实例模板化类?



我有一个模板化的gpio类:

template <gpio_types::port_t PORT, uint32_t PIN>
class gpio {};

我想创建一个将gpio实例作为模板的类。问题出在下面的行中。

template <uart_types::flexcomm PERIPH, int RX_BUF_LEN, gpio<gpio_types::port_t PORT, uint32_t PIN> TX_PIN>
class uart_shared_n_block {};

最后,我想像这样使用它:

gpio<gpio_types::P1, 13> tx;
auto uart = uart_shared_n_block<uart_types::FC_4,2048,tx>();

如何正确模板化uart_shared_n_block类?

这个

gpio<gpio_types::P1, 13> tx;

tx声明为类型<gpio<gpio_types::P1, 13>>的对象。gpio<gpio_types::P1, 13>不是时间,而是具体类型。如果要将该类型作为参数传递给uart_shared_n_block,则为:

template <uart_types::flexcomm PERIPH, int RX_BUF_LEN, 
typename T>
class uart_shared_n_block {};

然后你可以通过以下方式实例化它

auto uart = uart_shared_n_block<uart_types::FC_4,2048,gpio<gpio_types::P1, 13>>();

auto uart = uart_shared_n_block<uart_types::FC_4,2048,decltype(tx)>();

如果你真的想传递一个实例,而不是一个类型,那么我;)误解了这个问题。

最新更新