我有以下功能:
uint_fast32_t write(const std::vector<std::byte>& bytes_to_write) const
{
...
}
Run Code Online (Sandbox Code Playgroud)
我想用可变数量std::bytes 来称呼它:
std::byte low_byte { 0 };
std::byte hi_byte{ GPIO_PINS::GPIOH0 | GPIO_PINS::GPIOH3 };
write({
INST::SET_LOW,
low_byte,
0xFB,
INST::SET_HI,
hi_byte,
0xFF
});
Run Code Online (Sandbox Code Playgroud)
(INST和GPIO_PINS是enum class : byte-介意的区别byte(实际上unsigned char),并std::byte在这方面。)
但这不会编译。我必须按以下方式调整通话:
write({
std::byte(INST::SET_LOW),
low_byte,
std::byte(0xFB),
std::byte(INST::SET_HI),
hi_byte,
std::byte(0xFF)
});
Run Code Online (Sandbox Code Playgroud)
我的问题是,是否有一种方法可以消除对std::byte构造函数(或static_cast)的需求?
也许一些隐式操作符重载或类似?
“有没有办法将枚举类隐式转换为std :: byte?” -不
类型安全的枚举不允许隐式转换为其他类型。您必须使用演员表。
另请参阅:https : //en.cppreference.com/w/cpp/language/enum