Auto Prototype Generation Strikes Again

So, this dead-simple code illustrates the problem quite well:

template <typename VarType>
void printVar (VarType val) {
  Serial.println(val);
}

void setup() {
  printVar(7);
  printVar(3.14159);
  printVar("hello world");
}

void loop() {
}

As-is, it produces the compiler error:

Template:2: error: variable or field 'printVar' declared void

 void printVar (VarType val){

                ^

Template:2: error: 'VarType' was not declared in this scope

exit status 1
variable or field 'printVar' declared void

If you move the template to a .h file and include it, all is well. I assume this is another case of the Arduino IDE trying to be “helpful” by generating what it thinks are the correct prototypes and messing things up. That can usually be fixed by supplying your own prototypes. But, not sure what to do in this case since the template IS the prototype.

Copy and pasted your code into a sketch, using Arduino v1.8.9 and Arduino Uno. It compiled and ran. That is, after I added the Serial.begin(9600) command.

OK, thanks. I'm on v1.8.5. Glad to see there has been progress.

OK, thanks. I'm on v1.8.5. Glad to see there has been progress.

Err...
I think you missed the point DBMcDonald was making, the point was not the newer version of the IDI fixes the problem, the point was that you missed out Serial.begin(9600);

(Unless DBMcDonald tells me I'm mistaken of course!)

PerryBebbington:
Err...
I think you missed the point DBMcDonald was making, the point was not the newer version of the IDI fixes the problem, the point was that you missed out Serial.begin(9600);

(Unless DBMcDonald tells me I'm mistaken of course!)

Err...
Think again. Lack of Serial.begin has NOTHING to do with a compilation error. It would only be required for the code to execute properly after a successful compilation -- which I did not have due to my older IDE version.