当您想访问闪存时,您必须编写如下内容:
constexpr uint8_t n PROGMEM = 10;
auto x = pgm_read_byte(&n);
我不喜欢这种访问内存的方式。我想以相同的方式访问每种类型的内存(RAM、闪存、EEPROM……)。我想写这样一个更好的东西:
constexpr uint8_t n = 10; // constexpr tells the compiler:
// eh! I'm not planning to write in this variable
// so you can put it in flash memory
auto x = n; // copy n into x (but x is uint8_t, not constexpr)
我天真地尝试像这样实现:
namespace Progmem{
class uint8_t{
constexpr uint8_t(::uint8_t x):v{x}{}
// TODO: operator uint8_t() const {return pgm_read_byte(&v);}
// private:
::uint8_t v PROGMEM;
};
}
并以这种方式进行测试:
constexpr Progmem::uint8_t n = 10;
auto x = pgm_read_byte(&(n.v));
它可以编译,但 x 中的数字存储不正确。
我该如何写这门课?
谢谢你。