如何从流输入到枚举类型?
我可以这样做
unsigned int sex = 0;
stream >> sex;
student.m_bio.sex = static_cast<Sex>(sex);
除此以外?
inline std::istream & operator>>(std::istream & str, Sex & v) {
unsigned int sex = 0;
if (str >> sex)
v = static_cast<Sex>(sex);
return str;
}
如果要确保该值有效,可以执行以下操作:
enum Sex {
Male,
Female,
Sex_COUNT
};
inline std::istream & operator>>(std::istream & str, Sex & v) {
unsigned int sex = 0;
if (!(str >> sex))
return str;
if (sex >= Sex_COUNT) {
str.setstate(str.rdstate() | std::ios::failbit);
return str;
}
v = static_cast<Sex>(sex);
return str;
}
在这里以更一般的形式提出了这个问题:How to read enums from a std::istream in a generic fashion。OP 几乎有一个可行的解决方案;他只是遇到了一些麻烦const
,还有一些不必要的尖括号。这是充实的工作解决方案:
#include <iostream>
#include <type_traits>
enum enSide { eLeft, eRight };
enum enType { eConUndefined, eConRoom };
template<typename Enum>
class EnumReader
{
Enum& e_;
friend std::istream& operator>>(std::istream& in, const EnumReader& val) {
typename std::underlying_type<Enum>::type asInt;
if (in >> asInt) val.e_ = static_cast<Enum>(asInt);
return in;
}
public:
EnumReader(Enum& e) : e_(e) {}
};
template<typename Enum>
EnumReader<Enum> read_enum(Enum& e)
{
return EnumReader<Enum>(e);
}
class MyClass {
enSide mSide;
enType mType;
int mTargetId;
public:
friend std::istream& operator>>(std::istream& in, MyClass& val) {
in >> read_enum(val.mSide) >> read_enum(val.mType) >> val.mTargetId;
return in;
}
};
read_enum
函数模板在这里和标准库中的用途相同:std::make_pair
它std::make_shared
让我们可以省去尖括号。你也可以写得很好in >> EnumReader<enSide>(val.mSide) >> EnumReader<enType>(val.mType)
,但这更多的是打字(双关语)。
据称,一些供应商的标准库在std::underlying_type
其<type_traits>
标头中仍然缺失。如果您有这些不完整的库之一,您可以使用如何知道类枚举的基础类型中列出的解决方法之一?.
这不漂亮,但应该这样做
stream >> reinterpret_cast<std::underlying_type<Sex>::type &>(student.m_bio.sex);
干杯,CC