First, I am aware of those SO posts, but they do not answer my question (or if they do, I do not understand):
What is template<> inline bla bla
My question is the following: what is the reason why some of those cases work, and one fails with regards to template specialization? What are the mechanisms at stake? (this code was used on Arduino 1.8, but I guess it is general C++ features?).
Case 1: this works fine, including template specialization:
serial output:
specialized template bla!!
general template: 10
in main.ino:
#include "template.h"
const int i = 10;
void setup(){
Serial.begin(115200);
delay(10);
Serial.println("booted");
}
void loop(){
template_print("bla!!");
template_print(i);
Serial.println();
delay(5000);
}
in template.h:
#ifndef TEMPLATE
#define TEMPLATE
template <typename T>
inline void template_print(T data){
Serial.print("general template: ");
Serial.println(data);
}
template <>
inline void template_print(const char * data){
Serial.print("specialized template ");
Serial.println(data);
}
#endif
Case 2: this works fine, including template specialization.
in main.ino: the same.
in template.h:
#ifndef TEMPLATE
#define TEMPLATE
template <typename T>
void template_print(T data){
Serial.print("general template: ");
Serial.println(data);
}
template <>
void template_print(const char * data){
Serial.print("specialized template ");
Serial.println(data);
}
#endif
Case 3: this works fine
in main.ino: the same
in template.h:
#ifndef TEMPLATE
#define TEMPLATE
template <typename T>
void template_print(T data){
Serial.print("general template: ");
Serial.println(data);
}
#endif
in template .cpp:
#include <Arduino.h>
#include "template.h"
template <>
void template_print(const char * data){
Serial.print("specialized template ");
Serial.println(data);
}
Case 4: this compiles, but the template specialization does not work:
Serial output:
general template: bla!!
general template: 10
main.ino: the same as case 1
template.h: the same as case 3
template.cpp:
#include <Arduino.h>
#include "template.h"
template <>
inline void template_print(const char * data){
Serial.print("specialized template ");
Serial.println(data);
}
Edit
I had made a mistake, actually case 2 works.