Does the ardiuno compiler fully support templates?

I am trying to use templates to reduce the amount of duplicate code but cannot get the following code to compile.

template <class T>
class MyTemplate {
public:
    virtual T doIt(T it) = 0;
    virtual ~MyTemplate() = default;  // Ensure a virtual destructor for proper cleanup
};

class StringTemplate : public MyTemplate<String> {
public:
    String doIt(String it) override {
        Serial.println(it);
        return it;
    }
};

class IntTemplate : public MyTemplate<int> {
public:
    int doIt(int it) override {
        Serial.println(it);
        return it;
    }
};

StringTemplate strTemplate;
IntTemplate intTemplate;

template <class T>
void process(MyTemplate<T>& templateInstance, T value) {
    templateInstance.doIt(value);
}

void setup() {
    Serial.begin(9600);

    process(strTemplate, String("Hello, world!"));
    process(intTemplate, 42);
}

I think that it is valid C++ code, but I get the following errors

/home/hangstrap/Arduino/template_tutorial/template_tutorial.ino:28:25: error: 'T' was not declared in this scope
void process(MyTemplate& templateInstance, T value) {
^
/home/hangstrap/Arduino/template_tutorial/template_tutorial.ino:28:25: note: suggested alternative: 'Tc'
void process(MyTemplate& templateInstance, T value) {
^
Tc
/home/hangstrap/Arduino/template_tutorial/template_tutorial.ino:28:26: error: template argument 1 is invalid
void process(MyTemplate& templateInstance, T value) {
^
/home/hangstrap/Arduino/template_tutorial/template_tutorial.ino:28:47: error: 'T' has not been declared
void process(MyTemplate& templateInstance, T value) {
^
/home/hangstrap/Arduino/template_tutorial/template_tutorial.ino: In function 'void process(int&, int)':
/home/hangstrap/Arduino/template_tutorial/template_tutorial.ino:29:22: error: request for member 'doIt' in 'templateInstance', which is of non-class type 'int'
templateInstance.doIt(value);
^~~~

exit status 1

Thanks for your help

I suspect that the Arduino builder does not like what you're doing (not your fault). If so, I strongly suggest that you move your class stuff to .h and .cpp file and include the .h file.

For which board are you compiling; I can try later today to see if the issue is indeed as above and that suggestion that I made works.

Note:
I'm not much of a C++ guy.

OK, the builder does not seem to be the problem (or not on its own). In which case I do not know.

The original code was using *.H and *.c++ files.

For the post I reduced the code to a minimal example
Board is MRK WIFI 1010

I moved the code including the process function into a *.h file and it compiles.

What the hell is going on?

OK, curious and curioser....

Changing

template <class T> 
void process(MyTemplate<T>& templateInstance, T value) {

to

template <class T> void process(MyTemplate<T>& templateInstance, T value) {

fixes the problem

The Arduino IDE is trying to auto generate the functions prototypes for you. It does a good job at it in the general case but may fail miserably from time to time and you get cryptic error messages when compiling (because the IDE injected stuff in the wrong place and you don't see it).

here is one Github report

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.