Search code examples
c++classgpio

How do I template a class using an instance of another templated class?


I have a gpio class which is templated:

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

I want create a class that takes a gpio instance as a template. The problem is in the line below.

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 {};

In the end, I want to use it like this:

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

How to template the uart_shared_n_block class properly?


Solution

  • This

    gpio<gpio_types::P1, 13> tx;
    

    declares tx to be an object of type <gpio<gpio_types::P1, 13>>. gpio<gpio_types::P1, 13> is not a tempalte, it is a concrete type. If you want to pass that type as parameter to the uart_shared_n_block then, that would be:

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

    Then you can instantiate it via

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

    or

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

    If you actually want to pass an instance, not a type, then I misunderstood the question ;).