一个例子通常比长期解释更好.
您可以在Coliru上编译并运行此代码段.
(另一个前例子也可用)
#include <map>
#include <iostream>
struct MyClass
{
enum class …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中,因此使用迄今为止提出的解决方案不是一种选择.
如何enum class在C++ 11中输出a的值?在C++ 03中,它是这样的:
#include <iostream>
using namespace std;
enum A {
a = 1,
b = 69,
c= 666
};
int main () {
A a = A::c;
cout << a << endl;
}
Run Code Online (Sandbox Code Playgroud)
在c ++ 0x中,此代码无法编译
#include <iostream>
using namespace std;
enum class A {
a = 1,
b = 69,
c= 666
};
int main () {
A a = A::c;
cout << a << endl;
}
prog.cpp:13:11: error: cannot bind 'std::ostream' lvalue to 'std::basic_ostream<char>&&' …Run Code Online (Sandbox Code Playgroud) 我声明了枚举类型,
enum WeekEnum
{
Mon = 0;
Tue = 1;
Wed = 2;
Thu = 3;
Fri = 4;
Sat = 5;
Sun = 6;
};
Run Code Online (Sandbox Code Playgroud)
当我已经有项目值"0,1,等"时,如何获得项目名称"Mon,Tue等"
我已经有了这个功能
Log(Today is "2", enjoy! );
现在我想要下面的输出
今天是星期三,享受
考虑一下,我有以下enum课程:
enum class TestEnum
{
None = 0,
Foo,
Bar
};
Run Code Online (Sandbox Code Playgroud)
我想指定ostream operator ( << )这个enum类,所以我可以写:
std::cout << "This is " << TestEnum::Foo;
Run Code Online (Sandbox Code Playgroud)
并得到以下输出This is Foo。
我的问题是:
是否有存储枚举“名称说明符”的地方?(即对于enum class TestEnum 来说,它是None,Foo和Bar)所以我可以编写一个函数(或最多是函数模板)来指定ostream运算符,如下所示TestEnum:
std::ostream& operator<< ( std::ostream& os, TestEnum aEnum ) {
return std::string( aEnum.name() );
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,我是这样做的:
std::ostream& operator<< ( std::ostream& os, TestEnum aEnum ) {
switch( aEnum ) …Run Code Online (Sandbox Code Playgroud) 有没有办法显示枚举值的名称?说我们有:
enum fuits{
APPLE,
MANGO,
ORANGE,
};
main(){
enum fruits xFruit = MANGO;
...
printf("%s",_PRINT_ENUM_STRING(xFruit));
...
}
Run Code Online (Sandbox Code Playgroud)
使用预处理器
#define _PRINT_ENUM_STRING(x) #x
Run Code Online (Sandbox Code Playgroud)
将无法工作,因为我们需要获取变量'x'的值,然后将其转换为字符串.这在c/C++中是否可行?
请参阅文章中的以下代码,并不认为它是char*数组的标准C/C++语法.作为测试,Visual C++(visual studio 2005)和C++ Builder Rad XE都拒绝第二行.
不使用#defines,任何人都有任何技巧/提示,以保持enums和字符串数组排序同步而不诉诸STL?
更多的好奇心问题.
enum TCOLOR { RED, GREEN, BLUE };
char *TNCOLOR[] = { [RED]="Red", [GREEN]="Green", [BLUE]="Blue" };
Run Code Online (Sandbox Code Playgroud)
顺便说一句,这篇文章来自于相当陈旧,我相信这可能在海湾合作委员会下有效,但尚未经过测试.
extern "C" {
typedef struct Pair_s {
char *first;
char *second;
} Pair;
typedef struct PairOfPairs_s {
Pair *first;
Pair *second;
} PairOfPairs;
}
Pair pairs[] = {
{"foo", "bar"}, //this is fine
{"bar", "baz"}
};
PairOfPairs pops[] = {
{{"foo", "bar"}, {"bar", "baz"}}, //How can i create an equivalent of this NEATLY
{&pairs[0], &pairs[1]} //this is not considered neat (imagine trying to read a list of 30 of these)
};
Run Code Online (Sandbox Code Playgroud)
我怎样才能实现上面的样式声明语义?
我在测试中遇到了这个问题.
我知道我可以这样做:
enum class Color { red, green = 1, blue };
Color c = Color::blue;
if( c == Color::blue )
cout << "blue\n";
Run Code Online (Sandbox Code Playgroud)
但是,当我更换cout << "blue\n";使用cout << Color::green,它甚至不进行编译.为什么不编译?