为什么C ++ 11'auto'关键字对静态成员不起作用? [英] Why doesn't the C++11 'auto' keyword work for static members?

查看:447
本文介绍了为什么C ++ 11'auto'关键字对静态成员不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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 +


[

C ++ 11:7.1.6.4]:



1 code> type-specifier 表示要声明的变量的类型应从其初始化程序中推导出,或者函数声明符应包含一个 trailing-return-type



2 auto



(8.3.5)的函数声明符。 sup> 3 否则,变量的类型从其初始化器中推导出来。要声明的变量的名称不应出现在初始化器表达式中。在块(6.3),命名空间范围(3.3.6)和 for-init-statement中声明变量时允许使用 auto / em>(6.5.3)。 auto 将作为 decl-specifier-seq 声明中的声明说明符 -specifier-seq 后跟一个或多个初始化声明符,每个初始化声明符都应有一个非空的初始化符



4 auto / em>中的 type-specifier-seq 中的选择语句(6.4)或迭代语句(6.5)的 > for-range-declaration中的 new-type-id type-id new-expression >在类定义(9.4.2)的 member-specification 中声明具有括号或初始值化程序的静态数据成员< 。



5 在上下文中未明确使用 auto


这很难证明一个负面的,但没有明确的规则。



但是,相同的规则意味着以下 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:

[C++11: 7.1.6.4]:

1 The auto type-specifier signifies that the type of a variable being declared shall be deduced from its initializer or that a function declarator shall include a trailing-return-type.

2 The auto type-specifier may appear with a function declarator with a trailing-return-type (8.3.5) in any context where such a declarator is valid.

3 Otherwise, the type of the variable is deduced from its initializer. The name of the variable being declared shall not appear in the initializer expression. This use of auto is allowed when declaring variables in a block (6.3), in namespace scope (3.3.6), and in a for-init-statement (6.5.3). auto shall appear as one of the decl-specifiers in the decl-specifier-seq and the decl-specifier-seq shall be followed by one or more init-declarators, each of which shall have a non-empty initializer.

4 The auto type-specifier can also be used in declaring a variable in the condition of a selection statement (6.4) or an iteration statement (6.5), in the type-specifier-seq in the new-type-id or type-id of a new-expression (5.3.4), in a for-range-declaration, and in declaring a static data member with a brace-or-equal-initializer that appears within the member-specification of a class definition (9.4.2).

5 A program that uses auto in a context not explicitly allowed in this section is ill-formed.

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'关键字对静态成员不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆