【发布时间】:2013-01-11 19:22:53
【问题描述】:
class Foo {
public:
static const char *constant_string;
};
auto Foo::constant_string = "foo";
int main(void) {
};
编译:gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3 像这样:
gcc -std=c++0x ./foo.cc
./foo.cc:6:11: error: conflicting declaration ‘auto Foo::constant_string’
./foo.cc:3:22: error: ‘Foo::constant_string’ has a previous declaration as ‘const char* Foo::constant_string’
./foo.cc:6:11: error: declaration of ‘const char* Foo::constant_string’ outside of class is not definition [-fpermissive]
这是 auto 关键字的预期行为,还是 gcc+ 中的错误
【问题讨论】:
-
"foo"并不是真正的const char*,而是const char[4]。 -
不确定你想在这里用 auto 实现什么。 constant_string 已经被声明,静态初始化应该没有 auto 就可以了。