当我不控制源时,如何"add aliases"枚举值?
How can I "add aliases" to enum values when I don't control the source?
(后续问题)
我正在写一个使用库的程序,库的头文件定义了
enum foo : unsigned { first_foo, second_foo };
现在,我想给foo
值添加一些别名。如果我能控制库的源代码,我会写:
enum foo : unsigned {
first_foo,
second_foo,
best_foo = first_foo,
worst_foo = second_foo,
oldest_foo = first_foo,
newest_foo = second_foo;
};
…但我控制不了源头所以,我想写:
enum bar : foo {
best_foo = first_foo,
worst_foo = second_foo,
oldest_foo = first_foo,
newest_foo = second_foo;
};
,但在c++中无效,因为foo不是整型。如果我尝试绕过它并使用底层类型:
enum bar : std::underlying_type<foo> { /* etc. etc. */ }
可以编译,但是-我没有得到foo类型的值,并且我得到了编译器关于bar
和foo
比较的警告。
我可以使用static const
变量:
static const auto best_foo = first_foo;
static const auto worst_foo = second_foo,
static const auto oldest_foo = first_foo,
static const auto newest_foo = second_foo;
但是我不想冒险让它们进入文本部分,或者符号表
那么,我该怎么办呢?
您可以完全单独定义enum bar
,然后重载bar和foo的一元+
操作符以返回foo:
constexpr foo operator + ( bar const input )
{
return static_cast <foo> ( input );
}
constexpr foo operator + ( foo const input )
{
return input;
}
相关文章:
- Visual Studio - Cmake Project - Add NetCDF
- 在 atomic() 中 ++、add operation 和 fetch_add() 有什么区别
- wxImageList::Add()始终返回0
- 函数 C::add() 中不允许将'C *'隐式转换为'A *'
- C++有"not equal compare and exchange"或"fetch add on not equal"吗?
- gRPC trace.cc 的方法 TraceFlagList::Add(TraceFlag* flag) 使其链接列表
- 如何将用户定义的格式标志"add" basic_ostream?
- 'Cannot add two pointers'添加带有 WCHAR 的 LPCWSTR
- 在创建完整对象之前编写 if-can-add check 方法的正确方法?
- CMake add library libpq (postgreSQL) mac c++ clion
- 收到错误:C3867 '<int>Template_class::add':非标准语法;将指针指向带有模板的函数时
- 呼叫'php :: Extension :: add()没有匹配函数
- 使用intel内联汇编程序对bigint-add进行进位编码
- C++ 中头文件中的 Add 方法出错
- 二进制'+=':未找到采用类型 'Add' C++ 的全局运算符
- 如果我们使用链表数组来实现哈希表,则可以以不需要遍历的方式实现"add"。这是真的还是假的?
- 为什么 cmake 在定义 CXX 时忽略 ADD(SYSTEM) 头文件?
- "QFileSystemWatcher: failed to add paths"但仍然有效
- LNK2019反对CArray Add、GetAt、GetSize,所有包含都存在
- 当我不控制源时,如何"add aliases"枚举值?