C ++ 11 - 声明非静态数据成员为'auto' [英] C++11 - declaring non-static data members as 'auto'

查看:335
本文介绍了C ++ 11 - 声明非静态数据成员为'auto'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++ 11允许将非静态数据成员声明为auto,如果它们在声明中初始化的话?例如:

Does C++11 allow declaring non-static data members as 'auto' if they are initialized in the declaration? For example:

struct S
{
    auto x = 5;  // in place of 'int x = 5;', which is definitely allowed
};

GCC 4.7拒绝上述代码,但接受 int x = 5;

GCC 4.7 rejects the above code, while it accepts int x = 5;.

假设这不是编译器错误,而是标准真的不允许它,为什么不呢?它将像声明局部变量 auto 一样有用。

Assuming this is not a compiler bug but rather the standard really doesn't allow it, why not? It would be just as useful as declaring local variables auto.

推荐答案

禁止非静态成员的规则在7.1.6.4第4节中:

The rule for prohibiting non-static members is in 7.1.6.4 clause 4:


自动类型说明符也可以用于声明变量在
中的选择语句(6.4)或迭代语句
(6.5)的条件,在类型说明符seq中的new-type-id或类型id的
new-expression(5.3.4),在for-range-declaration中声明一个
静态数据成员,并在成员中显示一个大括号或初始值等于 - 定义类的定义(9.4.2)。

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).

我发现它的原理是静态的在这里,这反映了James McNellis在评论中解释的方式。

I found the rationale for it being static here which reflects how James McNellis explains it in the comment.


一个全国性的机构不喜欢允许使用
非静态的自动类型说明符。从电子邮件到作者:

One national body dislikes allowing the auto type-specifier for non-statics. From an e-mail to the authors:

    template< class T >
    struct MyType : T {
      auto data = func();
      static const size_t erm = sizeof(data);
    };

为了确定X的布局,我们现在有两阶段名称查找和ADL。注意,func可以是类型或函数;
它可以在T中找到,MyType的命名空间,T实例化时关联的
命名空间(s),全局命名空间,
匿名命名空间或任何使用的命名空间指示。
谨慎我们可以抛出一些concept_map查找运气。
根据头包含的顺序,我甚至可能得到不同的结果为ADL,并打破单一定义规则 - 其中
不需要被诊断。

In order to determine the layout of X, we now have 2-phase name lookup and ADL. Note that func could be either a type or a function; it may be found in T, the namespace of MyType, the associated namespace(s) of T when instantiated, the global namespace, an anonymous namespace, or any namespaces subject to a using directive. With care we could probably throw some concept_map lookup in for luck. Depending on the order of header inclusion I might even get different results for ADL, and break the One Definition Rule - which is not required to be diagnosed.

由于这个争议,作者不再建议auto
允许非静态数据成员。

Because of this controversy, the authors no longer propose that auto be allowed for non-static data members.

因此,基本上取决于头包含的顺序,数据的类型可能非常不同。当然, auto x = 5; 不需要依赖于两阶段名称查找或ADL,但是,我假设他们使它成为一个

So, basically depending on the order of header inclusion, the type of data could be very different. Of course, auto x = 5; would not need to depend on 2-phase name lookup or ADL, however, I'm a assuming that they made it a "blanket" rule because otherwise, they would have to make individual rules for every use case which would make things very complicated.

在同一篇文章中,作者提出了消除这种限制,但是,在这种情况下, ,似乎这个建议已被拒绝,可能是由于上述理由,也使预期的行为可以是相同的,无论初始值是什么。

In the same paper, the author proposes eliminating this restriction, however, it seems this proposal has been rejected probably due to the above rationale and also so that expected behavior can be the same no matter what the initializer is.

这篇关于C ++ 11 - 声明非静态数据成员为'auto'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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