I have asked a similar question before, but I realize that I can\'t make heads or tails of the macrology and templateness. I\'m a C (rather than C++) programmer.
Wha
There are no templates involved, only function overloading. The F()
macro does two things:
uses PSTR
to ensure that the literal string is stored in Flash memory (the code space rather than the data space). However, PSTR("some string")
cannot be printed because it would receive a simple char *
which represents a base address of the string stored in Flash. Dereferencing that pointer would access some random characters from the same address in data. Which is why F()
also...
casts the result of PSTR()
to __FlashStringHelper*
. Functions such as print
and println
are overloaded so that, on receiving a __FlashStringHelper*
argument, they correctly dereference the characters in the Flash memory.
BTW. For th ESP32 library, both of these functions are defined in the following files:
# PSTR : ../Arduino/hardware/espressif/esp32/cores/esp32/pgmspace.h
# F : ../Arduino/hardware/espressif/esp32/cores/esp32/WString.h
and the F(x):
// an abstract class used as a means to proide a unique pointer type
// but really has no body
class __FlashStringHelper;
#define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
...
Also for ESP32, PSTR(x)
is not needed and is just x: #define PSTR(s) (s)
.