我有一个我经常用于枚举包装的类,但是这需要一个cpp文件。谁能告诉我如何使用constexpr在枚举类包装类中定义静态constexpr值
头文件如下:
// Extend-able in the future
class CDUVariable {
public:
enum Value {
baud, firmware, type, serial
};
static const CDUVariable Baud, Firmware, Type, Serial;
/// Comparison operator (used for strict weak ordering).
bool operator<(const CDUVariable& rhs) const {
return mValue < rhs.mValue;
}
/// Integral operator cast for switch statements (cast to named enum).
operator const Value() const {
return mValue;
}
/// Serialized version of the enum.
std::string getStringVal() const {
return mStringVal;
}
static const std::set<CDUVariable>& getValues() {
static std::set<CDUVariable> gValues;
if (gValues.empty()) {
gValues.insert(Baud);
gValues.insert(Firmware);
gValues.insert(Type);
gValues.insert(Serial);
}
return gValues;
}
static CDUVariable valueOf(const std::string& rStringVal) {
for (const auto& next : getValues()) {
if (next.getStringVal() == rStringVal) {
return next;
}
}
throw std::invalid_argument(
"Illegal Argument: " + rStringVal);
}
private:
CDUVariable(const Value& rValue, const std::string& rStringVal)
: mValue(rValue)
, mStringVal(rStringVal)
{}
Value mValue;
std::string mStringVal;
};
和cpp文件是:
const CDUVariable CDUVariable::Baud(baud, "0");
const CDUVariable CDUVariable::Firmware(firmware, "1");
const CDUVariable CDUVariable::Type(type, "2");
const CDUVariable CDUVariable::Serial(serial, "3");
我想知道是否可以使用新的constexpr语法初始化头文件中的所有内容。我遇到了语法问题。
我试过前缀修改标题如下:
// Extend-able in the future
class CDUVariable {
public:
constexpr enum Value {
baud, firmware, type, serial
};
constexpr static CDUVariable Baud(baud, "0") Firmware(firmware, "1"), Type(type, "2"), Serial(serial, "3");
但是这最终给了我一堆错误。我想知道如何迁移到新的constexpr语法,并且这样做的好处在于,我可以发布仅包含这些枚举类型类的仅包含头文件的库。
2>c:\users\johnc\main\app739\include\vcdu\vcduconstants.h(107): error C2061: syntax error: identifier 'baud'
2>c:\users\johnc\main\app739\include\vcdu\vcduconstants.h(107): error C3646: 'Firmware': unknown override specifier
2>c:\users\johnc\main\app739\include\vcdu\vcduconstants.h(107): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
2>c:\users\johnc\main\app739\include\vcdu\vcduconstants.h(107): error C2059: syntax error: '('
2>c:\users\johnc\main\app739\include\vcdu\vcduconstants.h(147): error C2143: syntax error: missing ')' before ';'
2>c:\users\johnc\main\app739\include\vcdu\vcduconstants.h(147): error C2098: unexpected token after data member 'mValue'
2>c:\users\johnc\main\app739\include\vcdu\vcduconstants.h(147): error C2059: syntax error: ')'
2>c:\users\johnc\main\app739\include\vcdu\vcduconstants.h(111): error C2065: 'mValue': undeclared identifier
2>c:\users\johnc\main\app739\include\vcdu\vcduconstants.h(111): error C2039: 'mValue': is not a member of 'CDUVariable'
2>c:\users\johnc\main\app739\include\vcdu\vcduconstants.h(102): note: see declaration of 'CDUVariable'
2>c:\users\johnc\main\app739\include\vcdu\vcduconstants.h(116): error C2065: 'mValue': undeclared identifier
2>c:\users\johnc\main\app739\include\vcdu\vcduconstants.h(126): error C3867: 'CDUVariable::Baud': non-standard syntax; use '&' to create a pointer to member
2>c:\users\johnc\main\app739\include\vcdu\vcduconstants.h(127): error C2065: 'Firmware': undeclared identifier
2>c:\users\johnc\main\app739\include\vcdu\vcduconstants.h(128): error C2065: 'Type': undeclared identifier
2>c:\users\johnc\main\app739\include\vcdu\vcduconstants.h(129): error C2065: 'Serial': undeclared identifier
2>c:\users\johnc\main\app739\include\vcdu\vcduconstants.h(146): error C2614: 'CDUVariable': illegal member initialization: 'mValue' is not a base or member
2>c:\users\johnc\main\app739\src\vcdu\vcduconstants.cpp(165): error C2350: 'CDUVariable::Baud' is not a static member
2>c:\users\johnc\main\app739\src\vcdu\vcduconstants.cpp(165): note: see declaration of 'CDUVariable::Baud'
2>c:\users\johnc\main\app739\src\vcdu\vcduconstants.cpp(165): error C2248: 'CDUVariable::CDUVariable': cannot access private member declared in class 'CDUVariable'
2>c:\users\johnc\main\app739\include\vcdu\vcduconstants.h(143): note: see declaration of 'CDUVariable::CDUVariable'
2>c:\users\johnc\main\app739\include\vcdu\vcduconstants.h(102): note: see declaration of 'CDUVariable'
2>c:\users\johnc\main\app739\src\vcdu\vcduconstants.cpp(166): error C2039: 'Firmware': is not a member of 'CDUVariable'
2>c:\users\johnc\main\app739\include\vcdu\vcduconstants.h(102): note: see declaration of 'CDUVariable'
2>c:\users\johnc\main\app739\src\vcdu\vcduconstants.cpp(166): error C2065: 'firmware': undeclared identifier
2>c:\users\johnc\main\app739\src\vcdu\vcduconstants.cpp(167): error C2039: 'Type': is not a member of 'CDUVariable'
2>c:\users\johnc\main\app739\include\vcdu\vcduconstants.h(102): note: see declaration of 'CDUVariable'
2>c:\users\johnc\main\app739\src\vcdu\vcduconstants.cpp(167): error C2065: 'type': undeclared identifier
2>c:\users\johnc\main\app739\src\vcdu\vcduconstants.cpp(168): error C2039: 'Serial': is not a member of 'CDUVariable'
2>c:\users\johnc\main\app739\include\vcdu\vcduconstants.h(102): note: see declaration of 'CDUVariable'
2>c:\users\johnc\main\app739\src\vcdu\vcduconstants.cpp(168): error C2065: 'serial': undeclared identifier
2>Generating Code...
你曾经想复制或分配CDUVariable吗?什么是典型的用例? (我怀疑,我们可能正在处理一个XY问题。) – Walter
@Walter典型的用例是它对待这些枚举类的类,比如它们的java等价物。我的想法是,我可以做像'auto cduVariable = CDUVariable(CDUVariable :: valueOf(static_cast(data))',我可以从原始数据查找枚举值,也可以getStringVal并发送我尝试将std :: string改为const char *来解决非constexpr std :: string的问题,但是我无法得到仅用于编译头文件的版本 –
johnco3