Hello
I want to know if it's possible to do with constexpr the following.
#define STR_HELPER(x...) #x
#define STR(x) STR_HELPER(x)
#define MY_FULLNAMED_VARIABLE_FLOAT 37.5
String tmp = F("what's in flash mem:" + STR(MY_FULLNAMED_VARIABLE_FLOAT) + "the value or the name of the constant?");
In this case, I do have what I want:
tmp = "what's in flash mem:37.5the value or the name of the constant?";
If I replace the code #define MY_FULLNAMED_VARIABLE_FLOAT 37.5
by constexpr float MY_FULLNAMED_VARIABLE_FLOAT=37.5;
then, I get
tmp = "what's in flash mem:MY_FULLNAMED_VARIABLE_FLOATthe value or the name of the constant?";
constexpr float MY_FULLNAMED_VARIABLE_FLOAT=37.5;
String tmp = F("what's in flash mem:" MY_FULLNAMED_VARIABLE_FLOAT "the value or the name of the constant?");
Does not compile.
This compile, but it's not the same thing:
String tmp = F("what's in flash mem:") + MY_FULLNAMED_VARIABLE_FLOAT +F("the value or the name of the constant?");
Lose the # in the macro if you want to see the value instead.
The macro was only there to try to make it compile and produce desired behaviour
As far as I can tell the String class has no support for text strings to remain resident in Flash (never use SRAM). Which means using the F-macro serves no purpose with whatever it is you're trying to do. I suggest you try again without the F-macro.
As far as your code is concerned, on an AVR processor that has separate address spaces for the flash and ram, neither of these will store the variable or the variable name in flash memory. If the variable is not initialized away during optimization, the value will be in flash, but will be copied to ram during the initialization of the code, before even setup() is executed, and your code will never access it from flash. The variable name is never present in the compiled code, except when you specifically store it as text.
As @Delta_G noted, the #define is a pre-compile search/replace, so anywhere MY_FULLNAMED_VARIABLE_FLOAT appears it will be replaced by 37.5 before the code is compiled. The constexpr creates a float that will be stored in ram (since you did not specify PROGMEM when declaring the variable), but may not be stored in ram, if the compiler can optimize the code in such a way that the value is embedded in the code itself.