const char *pStr = PSTR("Hello"); // . // error: statement-expressions are not allowed outside functions nor in template-argument lists int main() {…}
int main() { char dest[20]; strcpy_P(dest, "Hello world!"); }
int main() { char dest[20]; strcpy_P(dest, PSTR("Hello world!")); }
const char *pStr = PSTR("Hello"); // . // error: statement-expressions are not allowed outside functions nor in template-argument lists int main() {…}
extern const char PROGMEM caption1[]; const char caption1[] = "Hello"; const char *pStr = caption1;
template <char value> struct ProgmemChar { static const char PROGMEM v; }; template <char value> const char ProgmemChar<value>::v = value; const char *pChar = &(ProgmemChar<'a'>::v);
template <char ch1, char ch2, char ch3, char ch4, char ch5> struct ProgmemString { static const char PROGMEM v[5]; }; template <char ch1, char ch2, char ch3, char ch4, char ch5> const char ProgmemString<ch1, ch2, ch3, ch4, ch5>::v[5] = {ch1, ch2, ch3, ch4, ch5}; const char *pStr = ProgmemString<'a', 'b', 'c', 'd', 0>::v;
template<size_t S, char... L>struct _Pstr;
#define SPLIT_TO_CHAR_4(STR) STR[0], STR[1], STR[2], STR[3]
"Hello world!"[0], "Hello world!"[1], "Hello world!"[2], "Hello world!"[3]
struct _CS { template<size_t n> constexpr _CS(const char (&s)[n]) :s(s), l(n){} constexpr char operator [](size_t i){return i < l ?s[i] :0;} const char *s = 0; const size_t l = 0; };
#define SPLIT_TO_CHAR(STR) _CS(STR)[0], _CS(STR)[1], …, _CS(STR)[N-1]
// template<size_t S, char... L>struct _PStr; // , . 10 #define ARGS01(P, S) P##00 S #define ARGS02(P, S) ARGS01(P, S),P##01 S #define ARGS03(P, S) ARGS02(P, S),P##02 S #define ARGS04(P, S) ARGS03(P, S),P##03 S #define ARGS05(P, S) ARGS04(P, S),P##04 S #define ARGS06(P, S) ARGS05(P, S),P##05 S #define ARGS07(P, S) ARGS06(P, S),P##06 S #define ARGS08(P, S) ARGS07(P, S),P##07 S #define ARGS09(P, S) ARGS08(P, S),P##08 S #define ARGS0A(P, S) ARGS09(P, S),P##09 S // ( 0 10 ). 0. template<char... L>struct _PStr<0x00, L...>{static const char PROGMEM v[];}; template<char... L>const char _PStr<0x00, L...>::v[] = {0}; template<ARGS01(char _,), char... L>struct _PStr<0x01, ARGS01(_,), L...>{static const char PROGMEM v[];}; template<ARGS01(char _,), char... L>const char _PStr<0x01, ARGS01(_,), L...>::v[] = {ARGS01(_,), 0}; template<ARGS02(char _,), char... L>struct _PStr<0x02, ARGS02(_,), L...>{static const char PROGMEM v[];}; template<ARGS02(char _,), char... L>const char _PStr<0x02, ARGS02(_,), L...>::v[] = {ARGS02(_,), 0}; template<ARGS03(char _,), char... L>struct _PStr<0x03, ARGS03(_,), L...>{static const char PROGMEM v[];}; template<ARGS03(char _,), char... L>const char _PStr<0x03, ARGS03(_,), L...>::v[] = {ARGS03(_,), 0}; template<ARGS04(char _,), char... L>struct _PStr<0x04, ARGS04(_,), L...>{static const char PROGMEM v[];}; template<ARGS04(char _,), char... L>const char _PStr<0x04, ARGS04(_,), L...>::v[] = {ARGS04(_,), 0}; template<ARGS05(char _,), char... L>struct _PStr<0x05, ARGS05(_,), L...>{static const char PROGMEM v[];}; template<ARGS05(char _,), char... L>const char _PStr<0x05, ARGS05(_,), L...>::v[] = {ARGS05(_,), 0}; template<ARGS06(char _,), char... L>struct _PStr<0x06, ARGS06(_,), L...>{static const char PROGMEM v[];}; template<ARGS06(char _,), char... L>const char _PStr<0x06, ARGS06(_,), L...>::v[] = {ARGS06(_,), 0}; template<ARGS07(char _,), char... L>struct _PStr<0x07, ARGS07(_,), L...>{static const char PROGMEM v[];}; template<ARGS07(char _,), char... L>const char _PStr<0x07, ARGS07(_,), L...>::v[] = {ARGS07(_,), 0}; template<ARGS08(char _,), char... L>struct _PStr<0x08, ARGS08(_,), L...>{static const char PROGMEM v[];}; template<ARGS08(char _,), char... L>const char _PStr<0x08, ARGS08(_,), L...>::v[] = {ARGS08(_,), 0}; template<ARGS09(char _,), char... L>struct _PStr<0x09, ARGS09(_,), L...>{static const char PROGMEM v[];}; template<ARGS09(char _,), char... L>const char _PStr<0x09, ARGS09(_,), L...>::v[] = {ARGS09(_,), 0}; template<ARGS0A(char _,), char... L>struct _PStr<0x0A, ARGS0A(_,), L...>{static const char PROGMEM v[];}; template<ARGS0A(char _,), char... L>const char _PStr<0x0A, ARGS0A(_,), L...>::v[] = {ARGS0A(_,), 0}; // struct _CS { template<size_t n> constexpr _CS(const char (&s)[n]) :s(s), l(n){} constexpr char operator [](size_t i){return i < l ?s[i] :0;} const char *s = 0; const size_t l = 0; }; // #define STR_UNION(...) __VA_ARGS__ // , , . SPS = StaticProgramString. #define SPS(T) STR_UNION(_PStr<_CS(T).l - 1, ARGS0A(_CS(T)[0x, ])>::v)
template <class T, const char *name> struct NamedType { T value; static const char *getName() { return name; } }; NamedType<int, SPS("")> var1 = {3};
Source: https://habr.com/ru/post/311874/
All Articles