Santos
Santos

Reputation: 37

Invalid conversion of pointer in template class instantiation

I declare the following template class on my .h file

enum class Hal_uart_id
{
    Uart = 0,
    Usart0,
    Usart1,
    Usart2,
    Usart3,
    UsbUart,
};

template <class T> 
class HalUART
{
    public:
        HalUART(Hal_uart_id uart_id);
    private:
        Hal_uart_id _uart_id;
        T* _p_uart; 
}

On my .cpp file I implement the class constructor as follows:

#include "hal_uart_common.h"
#include "Arduino.h"

template <class T> 
HalUART<T>::HalUART(Hal_uart_id id)
{
    _uart_id = id;
    switch (_uart_id)
    {
        case Hal_uart_id::Uart:
            _p_uart = &Serial;
            break;
        case Hal_uart_id::UsbUart:
            _p_uart = &SerialUSB;
            break;
        case Hal_uart_id::Usart0:
            _p_uart = &Serial1;
            break;
        case Hal_uart_id::Usart1:
            _p_uart = &Serial2;
            break;
        case Hal_uart_id::Usart3:
            _p_uart = &Serial3;
            break;
        default:
            break;
    }
}

At the end of my .cpp file I instantiate the template class with the USARTClass class

template class HalUART<USARTClass>;

I am getting the following compilation error and I cannot understand why or how to fix it:

src/hal/uart/hal_uart_sam3x.cpp: In instantiation of 'HalUART<T>::HalUART(Hal_uart_id) [with T = USARTClass]':
src/hal/uart/hal_uart_sam3x.cpp:58:16:   required from here
src/hal/uart/hal_uart_sam3x.cpp:23:21: error: invalid conversion from 'UARTClass*' to 'USARTClass*' [-fpermissive]
             _p_uart = &Serial;
             ~~~~~~~~^~~~~~~~~
src/hal/uart/hal_uart_sam3x.cpp:26:21: error: cannot convert 'Serial_*' to 'USARTClass*' in assignment
             _p_uart = &SerialUSB;
             ~~~~~~~~^~~~~~~~~~~~
*** [.pio/build/due/src/hal/uart/hal_uart_sam3x.cpp.o] Error 1

These object are defined in the Arduino core

UARTClass Serial;
USARTClass Serial1;
USARTClass Serial2;
USARTClass Serial3;
Serial_ SerialUSB;

For UART/USART class and objects definitions please see: https://github.com/arduino/ArduinoCore-sam/blob/master/cores/arduino/UARTClass.h https://github.com/arduino/ArduinoCore-sam/blob/master/cores/arduino/UARTClass.cpp

https://github.com/arduino/ArduinoCore-sam/blob/master/cores/arduino/USARTClass.h https://github.com/arduino/ArduinoCore-sam/blob/master/cores/arduino/USARTClass.cpp

https://github.com/arduino/ArduinoCore-sam/blob/master/cores/arduino/USB/USBAPI.h https://github.com/arduino/ArduinoCore-sam/blob/master/cores/arduino/USB/CDC.cpp

Upvotes: 1

Views: 164

Answers (1)

Jo&#227;o Paulo
Jo&#227;o Paulo

Reputation: 6680

Template types must be solved at compiler time. Instantiate your class with template class HalUART<UARTClass>; instead of USARTClass (once class USARTClass : public UARTClass;). And, also, in the Serial_ case, for instance, use if constexpr (c++17 only) with std::is_same:

case Hal_uart_id::UsbUart:
    if constexpr (std::is_same_v<T, Serial_>)
        _p_uart = &SerialUSB;
    break;

I recommend using if constexpr for the other cases too, once you instantiate your class with Serial_, for example, then you'll also be able to template class HalUART<USARTClass>;:

switch (_uart_id)
{
case Hal_uart_id::Uart:
    if constexpr (std::is_same_v<UARTClass, T>)
        _p_uart = &Serial;
    break;
case Hal_uart_id::UsbUart:
    if constexpr (std::is_same_v<T, Serial_>)
        _p_uart = &SerialUSB;
    break;
case Hal_uart_id::Usart0:
    if constexpr (std::is_same_v<USARTClass, T>)
        _p_uart = &Serial1;
    break;
case Hal_uart_id::Usart1:
    if constexpr (std::is_same_v<USARTClass, T>)
        _p_uart = &Serial2;
    break;
case Hal_uart_id::Usart3:
    if constexpr (std::is_same_v<USARTClass, T>)
        _p_uart = &Serial3;
    break;
default:
    break;
}

With this type checking, perhaps your switch may no longer be necessary. Try to evaluate this.

Consider using std::is_base_of if necessary.


If c++17 is not available, try specializing your function:

template <class T>
HalUART<T>::HalUART(Hal_uart_id id) {}

template <>
HalUART<UARTClass>::HalUART(Hal_uart_id id) {
    if (id == Hal_uart_id::Uart)
        _p_uart = &Serial;
}
template <>
HalUART<Serial_>::HalUART(Hal_uart_id id) {
    if (id == Hal_uart_id::UsbUart)
        _p_uart = &SerialUSB;
}
template <>
HalUART<USARTClass>::HalUART(Hal_uart_id id) {
    if (id == Hal_uart_id::Usart0)
        _p_uart = &Serial1;
    else if (id == Hal_uart_id::Usart1)
        _p_uart = &Serial2;
    else if (id == Hal_uart_id::Usart3)
        _p_uart = &Serial3;
}

live example

Upvotes: 1

Related Questions