C++ 如何使类定义之外的运算符重载优先?
我定义了以下运算符:C++ 如何使类定义之外的运算符重载优先?,c++,C++,我定义了以下运算符: std::vector<uint8_t> &operator<<(std::vector<uint8_t> &bytes, uint8_t b); std::vector<uint8_t> &operator<<(std::vector<uint8_t> &bytes, uint32_t b); 我可以使uint8\u t版本优先,这样我就不需要类型转换,例如,stat
std::vector<uint8_t> &operator<<(std::vector<uint8_t> &bytes, uint8_t b);
std::vector<uint8_t> &operator<<(std::vector<uint8_t> &bytes, uint32_t b);
我可以使
uint8\u t
版本优先,这样我就不需要类型转换,例如,static\u cast
来调用该操作符吗?您可以通过将第二个作为模板来破解它:
std::vector<uint8_t> &operator<<(std::vector<uint8_t> &bytes, uint8_t b);
template<class Y>
std::vector<uint8_t> &operator<<(std::vector<uint8_t> &bytes, Y b);
std::vector和operator这些重载有何不同?为什么不能只有一个呢?@RustyX这绝对是错误的。你在考虑标准的C规则,而不是C++。老实说,代码> StasyType Studio是实现你想要的真正干净的方法。不,不是。不,不是真的。谢谢-你能用一个小例子详细说明如何对Y类型进行静态断言吗?我认为你的想法很好。。。
std::vector<uint8_t> &operator<<(std::vector<uint8_t> &bytes, uint8_t b);
template<class Y>
std::vector<uint8_t> &operator<<(std::vector<uint8_t> &bytes, Y b);