例如:
int a = 12;
cout << typeof(a) << endl;
Run Code Online (Sandbox Code Playgroud)
预期产量:
int
Run Code Online (Sandbox Code Playgroud) 将enums作为标志处理可以在C#中通过[Flags]属性很好地工作,但是在C++中执行此操作的最佳方法是什么?
例如,我想写:
enum AnimalFlags
{
HasClaws = 1,
CanFly =2,
EatsFish = 4,
Endangered = 8
};
seahawk.flags = CanFly | EatsFish | Endangered;
Run Code Online (Sandbox Code Playgroud)
但是,我收到关于int/ enum转换的编译器错误.是否有更好的表达方式而不仅仅是直接的铸造?优选地,我不想依赖来自第三方库的构造,例如boost或Qt.
编辑:如答案中所示,我可以通过声明seahawk.flags为避免编译器错误int.但是,我想有一些机制来强制执行类型安全,所以有人不能写seahawk.flags = HasMaximizeButton.
能够在C++编译时创建和操作字符串有几个有用的应用程序.尽管可以在C++中创建编译时字符串,但是该过程非常麻烦,因为字符串需要声明为可变字符序列,例如
using str = sequence<'H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!'>;
Run Code Online (Sandbox Code Playgroud)
诸如字符串连接,子字符串提取等许多操作可以很容易地实现为对字符序列的操作.是否可以更方便地声明编译时字符串?如果没有,是否有一个提案可以方便地声明编译时字符串?
理想情况下,我们希望能够如下声明编译时字符串:
// Approach 1
using str1 = sequence<"Hello, world!">;
Run Code Online (Sandbox Code Playgroud)
或者,使用用户定义的文字,
// Approach 2
constexpr auto str2 = "Hello, world!"_s;
Run Code Online (Sandbox Code Playgroud)
哪里decltype(str2)有一个constexpr构造函数.方法1的混乱版本可以实现,利用您可以执行以下操作的事实:
template <unsigned Size, const char Array[Size]>
struct foo;
Run Code Online (Sandbox Code Playgroud)
但是,数组需要有外部链接,所以要使方法1起作用,我们必须编写如下内容:
/* Implementation of array to sequence goes here. */
constexpr const char str[] = "Hello, world!";
int main()
{
using s = string<13, str>;
return 0; …Run Code Online (Sandbox Code Playgroud) 假设我们有一些命名的枚举:
enum MyEnum {
FOO,
BAR = 0x50
};
Run Code Online (Sandbox Code Playgroud)
我搜索的是一个脚本(任何语言),它扫描我项目中的所有标题,并生成一个标题,每个枚举一个函数.
char* enum_to_string(MyEnum t);
Run Code Online (Sandbox Code Playgroud)
以及类似这样的实现:
char* enum_to_string(MyEnum t){
switch(t){
case FOO:
return "FOO";
case BAR:
return "BAR";
default:
return "INVALID ENUM";
}
}
Run Code Online (Sandbox Code Playgroud)
这个问题确实与typedefed枚举和未命名的C风格枚举有关.有人知道这个吗?
编辑:解决方案不应该修改我的源,除了生成的函数.枚举是在API中,因此使用迄今为止提出的解决方案不是一种选择.
我在一些库头文件中有一堆枚举类型,我正在使用,我想有一种方法将枚举值转换为用户字符串 - 反之亦然.
RTTI不会为我做这件事,因为'用户字符串'需要比枚举更具可读性.
一个强力解决方案将是一堆像这样的功能,但我觉得这有点像C样.
enum MyEnum {VAL1, VAL2,VAL3};
String getStringFromEnum(MyEnum e)
{
switch e
{
case VAL1: return "Value 1";
case VAL2: return "Value 2";
case VAL1: return "Value 3";
default: throw Exception("Bad MyEnum");
}
}
Run Code Online (Sandbox Code Playgroud)
我有一种直觉,认为使用模板有一个优雅的解决方案,但我还不能完全理解它.
更新:感谢您的建议 - 我应该明确说明枚举是在第三方库头中定义的,所以我不想更改它们的定义.
我现在的直觉是避免使用模板并执行以下操作:
char * MyGetValue(int v, char *tmp); // implementation is trivial
#define ENUM_MAP(type, strings) char * getStringValue(const type &T) \
{ \
return MyGetValue((int)T, strings); \
}
; enum eee {AA,BB,CC}; - exists in library header file
; …Run Code Online (Sandbox Code Playgroud) 如何让printf显示枚举类型变量的值?例如:
typedef enum {Linux, Apple, Windows} OS_type;
OS_type myOS = Linux;
Run Code Online (Sandbox Code Playgroud)
而我需要的是类似的东西
printenum(OS_type, "My OS is %s", myOS);
Run Code Online (Sandbox Code Playgroud)
必须显示字符串"Linux",而不是整数.
我想,首先我必须创建一个值索引的字符串数组.但我不知道这是否是最美妙的方式.有可能吗?
这是我想要做的:
typedef enum { ONE, TWO, THREE } Numbers;
Run Code Online (Sandbox Code Playgroud)
我正在尝试编写一个函数来执行类似于以下的切换案例:
char num_str[10];
int process_numbers_str(Numbers num) {
switch(num) {
case ONE:
case TWO:
case THREE:
{
strcpy(num_str, num); //some way to get the symbolic constant name in here?
} break;
default:
return 0; //no match
return 1;
}
Run Code Online (Sandbox Code Playgroud)
而不是在每种情况下定义,有没有办法使用枚举变量设置它,就像我在上面尝试做的那样?
如果我有这样的枚举
enum Errors
{ErrorA=0, ErrorB, ErrorC};
Run Code Online (Sandbox Code Playgroud)
然后我想打印到控制台
Errors anError = ErrorA;
cout<<anError;/// 0 will be printed
Run Code Online (Sandbox Code Playgroud)
但我想要的是文本"ErrorA",我可以不使用if/switch吗?
你有什么解决方案?
可能重复:
是否有一个简单的脚本将C++枚举转换为字符串?
我经常发现我需要将枚举转换为c ++中的字符串
我总是这样做:
enum Enum{ Banana, Orange, Apple } ;
char * getTextForEnum( int enumVal )
{
switch( enumVal )
{
case Enum::Banana:
return "bananas & monkeys";
case Enum::Orange:
return "Round and orange";
case Enum::Apple:
return "APPLE" ;
default:
return "Not recognized..";
}
}
Run Code Online (Sandbox Code Playgroud)
这样做是否有更好或更公认的习语?