请教个c++语法问题(在android源代码中看到)
是在android的JB代码的init_parser.c文件中:
#define KEYWORD(symbol, flags, nargs, func) \
[ K_##symbol ] = { #symbol, func, nargs + 1, flags, },
struct {
const char *name;
int (*func)(int nargs, char **args);
unsigned char nargs;
unsigned char flags;
} keyword_info[KEYWORD_COUNT] = {
[ K_UNKNOWN ] = { "unknown", 0, 0, 0 }, //这是什么语法?好像和lambda function有关?
#include "keywords.h"
};
其中,K_UNKNOWN是一个枚举类型,KEYWORD_COUNT是枚举类型中的最后一个变量。那些宏定义什么的都不是关键。
于是,我依样画葫芦的写了以下代码,可是用Android.mk编译出错了,,,
enum
{
AA,
BB,
COUNT
};
struct {
int m;
int count;
}aa[COUNT] = {
[ AA ] = { 2, 3 },
[ BB ] = { 5, 3 },
};
target C++: test <= main.cpp
main.cpp: In lambda function:
main.cpp:53:16: error: expected '{' before '=' token
main.cpp: In function 'int main(int, char**)':
main.cpp:53:16: warning: lambda expressions only available with -std=c++11 or -std=gnu++11 [enabled by default]
main.cpp:53:25: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
main.cpp:53:25: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
main.cpp:53:25: error: no match for 'operator=' in '{(._18)0u} = {2, 3}'
main.cpp:53:25: note: candidate is:
main.cpp:53:14: note: main(int, char**)::<lambda()>& main(int, char**)::<lambda()>::operator=(const main(int, char**)::<lambda()>&)
main.cpp:53:14: note: no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'const main(int, char**)::<lambda()>&'
main.cpp: In lambda function:
main.cpp:54:16: error: expected '{' before '=' token
main.cpp: In function 'int main(int, char**)':
main.cpp:54:16: warning: lambda expressions only available with -std=c++11 or -std=gnu++11 [enabled by default]
main.cpp:54:25: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
main.cpp:54:25: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
main.cpp:54:25: error: no match for 'operator=' in '{(._18)1u} = {5, 3}'
main.cpp:54:25: note: candidate is:
main.cpp:54:14: note: main(int, char**)::<lambda()>& main(int, char**)::<lambda()>::operator=(const main(int, char**)::<lambda()>&)
main.cpp:54:14: note: no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'const main(int, char**)::<lambda()>&'
make: *** [test/main.o] Error 1
[解决办法]
应该是Gcc的扩展,见以下:
http://hi.baidu.com/wangjianzhong0/item/9d3da8977a31b137326eebf8
[解决办法]
标准C89需要初始化语句的元素以固定的顺序出现,和被初始化的数组或结构体中的元素顺序一样。
在ISO C99中,你可以按任何顺序给出这些元素,指明它们对应的数组的下标或结构体的成员名,并且GNU C也把这作为C89模式下的一个扩展。这个扩展没有在GNU C++中实现。
为了指定一个数组下标,在元素值的前面写上“[index] =”。比如:
int a[6] = { [4] = 29, [2] = 15 };
相当于:
int a[6] = { 0, 0, 15, 0, 29, 0 };
[解决办法]
gcc的扩展。
# cat x.c
# include <stdio.h>
int main()
{
struct {
int a;
int b;
} foo[2] = {
[1] = { .b = 2, .a = 1, },
[0] = { 1, 2, },
};
return 0;
}
cc -Wall x.c -o x
不需要什么flags
[解决办法]
gcc对C的扩展,明显C和CPP是两种语言吧,C++也没有这种语法吧。