Arduino F(): what does it actually do
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 simplechar *
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 whyF()
also...casts the result of
PSTR()
to__FlashStringHelper*
. Functions such asprint
andprintln
are overloaded so that, on receiving a__FlashStringHelper*
argument, they correctly dereference the characters in the Flash memory.