本文介绍了为什么C ++ 11'auto'关键字对静态成员不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 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喜欢这样: / p> gcc -std = c ++ 0x ./foo.cc ./foo.cc:6:11:错误:冲突的声明'auto Foo :: constant_string' ./foo.cc:3:22:error:'Foo :: constant_string'有一个先前的声明为'const char * Foo :: constant_string' ./foo.cc:6:11:错误:在类外声明'const char * Foo :: constant_string'不是定义[-fpermissive] 这是 auto 关键字的预期行为,或gcc + 这很难证明一个负面的,但没有明确的规则。 但是,相同的规则意味着以下是 valid: struct Foo { static constexpr auto constant_string =foo; }; int main(){} code> Foo :: constant_string 是 char const * const ,而不是 ; 这是使用 auto 。) class Foo { public: static const char *constant_string;};auto Foo::constant_string = "foo";int main(void) {};Compiled with: gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3 like this: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]Is this intended behavior of the auto keyword, or a bug in gcc+ 解决方案 It's disallowed by the language:It's hard to prove a negative, but there's simply no explicit rule in the standard to allow auto in your case.However, the same rules mean that the following is valid:struct Foo { static constexpr auto constant_string = "foo";};int main() {}(Note that the type of Foo::constant_string is char const* const rather than, say, char const[3]; this is an effect of using auto.) 这篇关于为什么C ++ 11'auto'关键字对静态成员不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
05-27 21:15
查看更多