Suppose I have a static function which takes an enum and returns a cstring ptr for debugging.
The function can be constexpr but no guarantee is made that it can always be evaluated at compile time. Say it is running on a microcontroller and this is signalling events from a Bluetooth stack; e.g. device connected, disconnected, data in, etc, for context. So the parameter is not necessarily known at compile time.
Is there any value, meaning or difference in having the cstrings also defined as constexpr vs not?
A condensed example of what I mean:
#include <cstdio>
enum myEnum
{
EVENT1,
EVENT2,
EVENT3
};
static constexpr const char* foo(const myEnum i)
{
// Does having these as constexpr change anything?
/*constexpr*/ const char* text1 = "Text1";
/*constexpr*/ const char* text2 = "Text2";
/*constexpr*/ const char* text3 = "Text3";
/*constexpr*/ const char* textUndef = "TextUndef";
switch (i)
{
case EVENT1:
return text1;
case EVENT2:
return text2;
case EVENT3:
return text3;
default:
return textUndef;
}
}
int main()
{
const char* x = foo(EVENT1);
const char* y = foo(/*some value only known at runtime*/);
printf(x);
printf(y);
return 1;
}
I am compiling with gcc for cpp17 for an embedded microcontroller. Typically with either -Og or -Os.